From d5c5166fcfbdaec2297d68e1e47a869e3bf2049b Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 28 Oct 2011 15:06:39 +0100 Subject: [PATCH] use stack rather than vector and throw parse exceptions --- src/jalview/analysis/Rna.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/jalview/analysis/Rna.java b/src/jalview/analysis/Rna.java index ca3c6d5..ba18732 100644 --- a/src/jalview/analysis/Rna.java +++ b/src/jalview/analysis/Rna.java @@ -24,6 +24,7 @@ package jalview.analysis; import java.util.ArrayList; import java.util.Hashtable; +import java.util.Stack; import java.util.Vector; import jalview.datamodel.SequenceFeature; @@ -43,11 +44,9 @@ public class Rna * @return Array of SequenceFeature; type = RNA helix, begin is open base * pair, end is close base pair */ - public static SequenceFeature[] GetBasePairs(String line) public static SequenceFeature[] GetBasePairs(CharSequence line) throws WUSSParseException { - - Vector stack = new Vector(); + Stack stack = new Stack(); Vector pairs = new Vector(); int i = 0; @@ -57,14 +56,18 @@ public class Rna if ((base == '<') || (base == '(') || (base == '{') || (base == '[')) { - stack.addElement(i); + stack.push(i); } else if ((base == '>') || (base == ')') || (base == '}') || (base == ']')) { - Object temp = stack.lastElement(); - stack.remove(stack.size() - 1); + if (stack.isEmpty()) + { + // error whilst parsing i'th position. pass back + throw new WUSSParseException("Mismatched closing bracket", i); + } + Object temp = stack.pop(); pairs.addElement(temp); pairs.addElement(i); } @@ -187,3 +190,4 @@ public class Rna } } } + -- 1.7.10.2