Example #1
0
 /**
  * Transfer the contents of src to dest in a synchronized manner (since SimpleQueue doesn't have
  * internal synchronization).
  */
 public static void transfer(SimpleQueue<String> src, SimpleQueue<String> dest)
     throws InterruptedException {
   // Acquire the locks for src and dest.
   synchronized (src) {
     synchronized (dest) {
       // Remove each element from src and put it into dest.
       while (!src.isEmpty()) {
         dest.put(src.take());
       }
     }
   }
 }
  private ListRepresentation convertToListRepresentation() {
    SimpleQueue<ExpressionAtom> output = new SimpleQueue<ExpressionAtom>();
    SimpleStack<ExpressionAtom> stack = new SimpleStack<ExpressionAtom>();

    for (ExpressionAtom atom : infixExpression) {
      Utils.printAtom(atom);
      if (atom.getAtomType() == AtomType.OPERAND) {
        output.enqueue(atom);
      } else if (atom.getAtomType() == AtomType.OPERATOR
          && !atom.getVariablesOrOperator().equals("(")
          && !atom.getVariablesOrOperator().equals(")")) {
        while (!stack.isEmpty()) {
          ExpressionAtom peek = stack.peek();
          if (isTopwithHigerPrecedence(peek, atom)) {
            output.enqueue(stack.pop());
          } else break;
        }
        stack.push(atom);
      } else if (atom.getVariablesOrOperator().equals("(")) {
        stack.push(atom);
      } else if (atom.getVariablesOrOperator().equals(")")) {
        boolean isLeftFound = false;
        while (!stack.isEmpty()) {
          ExpressionAtom top = stack.pop();
          if (!top.getVariablesOrOperator().equals("(")) {
            output.enqueue(top);
          } else {
            isLeftFound = true;
            break;
          }
        }
        if (!isLeftFound) throw new RuntimeException("Parantheses are not balanced");
      }
    }

    while (!stack.isEmpty()) {
      ExpressionAtom top = stack.pop();
      if (top.getVariablesOrOperator().equals("(") || top.getVariablesOrOperator().equals(")")) {
        throw new RuntimeException("Parantheses are not balanced");
      }
      output.enqueue(top);
    }

    ListRepresentation temp = new ListRepresentation();
    temp.setNodeVal(new ExpressionAtom("", null, 0));
    SimpleStack<ListRepresentation> operands = new SimpleStack<ListRepresentation>();
    List<ListRepresentation> copy = new ArrayList<ListRepresentation>();

    System.out.println();

    while (!output.isEmpty()) {
      ExpressionAtom atom = output.dequeue();
      Utils.printAtom(atom);
      if (atom.getAtomType() == AtomType.OPERAND) {
        ListRepresentation operand = new ListRepresentation();
        operand.setNodeVal(atom);
        operands.push(operand);
      } else {
        if (atom.getVariablesOrOperator().equals("-")
            || atom.getVariablesOrOperator().equals("%")) {
          ListRepresentation peek = operands.peek();
          if (peek.operands.size() == 0) {
            peek.getNodeVal().setCoefficient(peek.getNodeVal().getCoefficient() * -1);
          } else {
            peek.setNegative(true);
          }

          if (atom.getVariablesOrOperator().equals("%")) {
            if (peek.operands.size() == 0) continue;
            else {
              continue;
            }
          }
          atom = new ExpressionAtom("+", AtomType.OPERATOR, 1);
        }
        ListRepresentation operator = new ListRepresentation();
        operator.setNodeVal(atom);

        ListRepresentation op = operands.pop();

        operator.operands.add(operands.pop());
        operator.operands.add(op);

        copy.add(operator);
        operands.push(operator);
        temp = operator;
      }
    }

    ListRepresentation cop = operands.pop();
    validate(cop, cop);
    listRepresentation = cop;

    return listRepresentation;
  }
 protected Void doInBackground(Void... voids) {
   queueMessageArray = SimpleQueue.recieveMessageIds(queueUrl);
   return null;
 }
Example #4
0
 public void add(T item) {
   super.add(item);
 }