@Test(expected = NullPointerException.class) public void parserShouldNotAcceptNull() { parser.toPrefix(null); fail(); }
public List<String> toPrefix(String in) { int indexIn = in.indexOf(" in "); if (indexIn == -1) { indexIn = in.indexOf(" IN "); } if (indexIn != -1) { int indexOpen = in.indexOf("(", indexIn); int indexClose = in.indexOf(")", indexOpen); String sub = in.substring(indexOpen, indexClose + 1); sub = sub.replaceAll(" ", ""); in = in.substring(0, indexOpen) + sub + in.substring(indexClose + 1); } List<String> stack = new ArrayList<String>(); List<String> output = new ArrayList<String>(); List<String> tokens = split(in); if (tokens.contains("between") || tokens.contains("BETWEEN")) { boolean found = true; boolean dirty = false; betweens: while (found) { for (int i = 0; i < tokens.size(); i++) { if ("between".equalsIgnoreCase(tokens.get(i))) { tokens.set(i, "betweenAnd"); tokens.remove(i + 2); dirty = true; continue betweens; } } found = false; } if (dirty) { for (int i = 0; i < tokens.size(); i++) { if ("betweenAnd".equals(tokens.get(i))) { tokens.set(i, "between"); } } } } for (String token : tokens) { if (isOperand(token)) { if (token.equals(")")) { while (openParanthesesFound(stack)) { output.add(stack.remove(stack.size() - 1)); } if (stack.size() > 0) { stack.remove(stack.size() - 1); } } else { while (openParanthesesFound(stack) && !hasHigherPrecedence(token, stack.get(stack.size() - 1))) { output.add(stack.remove(stack.size() - 1)); } stack.add(token); } } else { output.add(token); } } while (stack.size() > 0) { output.add(stack.remove(stack.size() - 1)); } return output; }
Parser { public List<String> toPrefix(String in) { int indexIn = in.indexOf(" in "); if (indexIn == -1) { indexIn = in.indexOf(" IN "); } if (indexIn != -1) { int indexOpen = in.indexOf("(", indexIn); int indexClose = in.indexOf(")", indexOpen); String sub = in.substring(indexOpen, indexClose + 1); sub = sub.replaceAll(" ", ""); in = in.substring(0, indexOpen) + sub + in.substring(indexClose + 1); } List<String> stack = new ArrayList<String>(); List<String> output = new ArrayList<String>(); List<String> tokens = split(in); if (tokens.contains("between") || tokens.contains("BETWEEN")) { boolean found = true; boolean dirty = false; betweens: while (found) { for (int i = 0; i < tokens.size(); i++) { if ("between".equalsIgnoreCase(tokens.get(i))) { tokens.set(i, "betweenAnd"); tokens.remove(i + 2); dirty = true; continue betweens; } } found = false; } if (dirty) { for (int i = 0; i < tokens.size(); i++) { if ("betweenAnd".equals(tokens.get(i))) { tokens.set(i, "between"); } } } } for (String token : tokens) { if (isOperand(token)) { if (token.equals(")")) { while (openParanthesesFound(stack)) { output.add(stack.remove(stack.size() - 1)); } if (stack.size() > 0) { stack.remove(stack.size() - 1); } } else { while (openParanthesesFound(stack) && !hasHigherPrecedence(token, stack.get(stack.size() - 1))) { output.add(stack.remove(stack.size() - 1)); } stack.add(token); } } else { output.add(token); } } while (stack.size() > 0) { output.add(stack.remove(stack.size() - 1)); } return output; } }
Parser { public List<String> toPrefix(String in) { int indexIn = in.indexOf(" in "); if (indexIn == -1) { indexIn = in.indexOf(" IN "); } if (indexIn != -1) { int indexOpen = in.indexOf("(", indexIn); int indexClose = in.indexOf(")", indexOpen); String sub = in.substring(indexOpen, indexClose + 1); sub = sub.replaceAll(" ", ""); in = in.substring(0, indexOpen) + sub + in.substring(indexClose + 1); } List<String> stack = new ArrayList<String>(); List<String> output = new ArrayList<String>(); List<String> tokens = split(in); if (tokens.contains("between") || tokens.contains("BETWEEN")) { boolean found = true; boolean dirty = false; betweens: while (found) { for (int i = 0; i < tokens.size(); i++) { if ("between".equalsIgnoreCase(tokens.get(i))) { tokens.set(i, "betweenAnd"); tokens.remove(i + 2); dirty = true; continue betweens; } } found = false; } if (dirty) { for (int i = 0; i < tokens.size(); i++) { if ("betweenAnd".equals(tokens.get(i))) { tokens.set(i, "between"); } } } } for (String token : tokens) { if (isOperand(token)) { if (token.equals(")")) { while (openParanthesesFound(stack)) { output.add(stack.remove(stack.size() - 1)); } if (stack.size() > 0) { stack.remove(stack.size() - 1); } } else { while (openParanthesesFound(stack) && !hasHigherPrecedence(token, stack.get(stack.size() - 1))) { output.add(stack.remove(stack.size() - 1)); } stack.add(token); } } else { output.add(token); } } while (stack.size() > 0) { output.add(stack.remove(stack.size() - 1)); } return output; } Parser(); }
Parser { public List<String> toPrefix(String in) { int indexIn = in.indexOf(" in "); if (indexIn == -1) { indexIn = in.indexOf(" IN "); } if (indexIn != -1) { int indexOpen = in.indexOf("(", indexIn); int indexClose = in.indexOf(")", indexOpen); String sub = in.substring(indexOpen, indexClose + 1); sub = sub.replaceAll(" ", ""); in = in.substring(0, indexOpen) + sub + in.substring(indexClose + 1); } List<String> stack = new ArrayList<String>(); List<String> output = new ArrayList<String>(); List<String> tokens = split(in); if (tokens.contains("between") || tokens.contains("BETWEEN")) { boolean found = true; boolean dirty = false; betweens: while (found) { for (int i = 0; i < tokens.size(); i++) { if ("between".equalsIgnoreCase(tokens.get(i))) { tokens.set(i, "betweenAnd"); tokens.remove(i + 2); dirty = true; continue betweens; } } found = false; } if (dirty) { for (int i = 0; i < tokens.size(); i++) { if ("betweenAnd".equals(tokens.get(i))) { tokens.set(i, "between"); } } } } for (String token : tokens) { if (isOperand(token)) { if (token.equals(")")) { while (openParanthesesFound(stack)) { output.add(stack.remove(stack.size() - 1)); } if (stack.size() > 0) { stack.remove(stack.size() - 1); } } else { while (openParanthesesFound(stack) && !hasHigherPrecedence(token, stack.get(stack.size() - 1))) { output.add(stack.remove(stack.size() - 1)); } stack.add(token); } } else { output.add(token); } } while (stack.size() > 0) { output.add(stack.remove(stack.size() - 1)); } return output; } Parser(); List<String> toPrefix(String in); List<String> split(String in); }
Parser { public List<String> toPrefix(String in) { int indexIn = in.indexOf(" in "); if (indexIn == -1) { indexIn = in.indexOf(" IN "); } if (indexIn != -1) { int indexOpen = in.indexOf("(", indexIn); int indexClose = in.indexOf(")", indexOpen); String sub = in.substring(indexOpen, indexClose + 1); sub = sub.replaceAll(" ", ""); in = in.substring(0, indexOpen) + sub + in.substring(indexClose + 1); } List<String> stack = new ArrayList<String>(); List<String> output = new ArrayList<String>(); List<String> tokens = split(in); if (tokens.contains("between") || tokens.contains("BETWEEN")) { boolean found = true; boolean dirty = false; betweens: while (found) { for (int i = 0; i < tokens.size(); i++) { if ("between".equalsIgnoreCase(tokens.get(i))) { tokens.set(i, "betweenAnd"); tokens.remove(i + 2); dirty = true; continue betweens; } } found = false; } if (dirty) { for (int i = 0; i < tokens.size(); i++) { if ("betweenAnd".equals(tokens.get(i))) { tokens.set(i, "between"); } } } } for (String token : tokens) { if (isOperand(token)) { if (token.equals(")")) { while (openParanthesesFound(stack)) { output.add(stack.remove(stack.size() - 1)); } if (stack.size() > 0) { stack.remove(stack.size() - 1); } } else { while (openParanthesesFound(stack) && !hasHigherPrecedence(token, stack.get(stack.size() - 1))) { output.add(stack.remove(stack.size() - 1)); } stack.add(token); } } else { output.add(token); } } while (stack.size() > 0) { output.add(stack.remove(stack.size() - 1)); } return output; } Parser(); List<String> toPrefix(String in); List<String> split(String in);  }