public void add(E e) {
   for (int i = 0; i < replicas; ++i) {
     Long hash = Long.valueOf(func.hash(e.toString() + i));
     circle.put(hash, e);
   }
   values.add(e);
 }
 public String toString2() {
   StringBuffer sb = new StringBuffer();
   for (E e : this) {
     sb.append(e.toString() + "\n");
   }
   return sb.toString();
 }
 public void remove(E e) {
   for (int i = 0; i < replicas; ++i) {
     Long hash = Long.valueOf(func.hash(e.toString() + i));
     circle.remove(hash);
   }
   values.remove(e);
 }
Exemple #4
0
  /**
   * Returns an enum instance for the requested value.
   *
   * @param pref error prefix
   * @param name name
   * @param names allowed names
   * @param <E> token type
   * @return enum
   * @throws BaseXException database exception
   */
  public static <E extends Enum<E>> E value(final String pref, final byte[] name, final E[] names)
      throws BaseXException {

    final String n = string(name);
    for (final E nm : names) if (n.equals(nm.toString())) return nm;
    throw new BaseXException("%: Unexpected element: \"%\".", pref, name);
  }
 @Override
 public List<String> getItems() {
   final List<String> items = new ArrayList<String>();
   for (final E item : queue) {
     items.add(item.toString());
   }
   return items;
 }
 @Override
 public String toString() {
   if (data == null) {
     return null;
   } else {
     return data.toString();
   }
 }
 /*
  * (non-Javadoc)
  *
  * @see java.util.AbstractCollection#toString()
  */
 @Override
 public String toString() {
   final StringBuilder result = new StringBuilder();
   for (E object : this) {
     result.append(object.toString());
   }
   return result.toString();
 }
Exemple #8
0
 public String convert(E input) throws IllegalArgumentException {
   String result;
   if (input == defaultValue || input == null) result = null;
   else {
     result = input.toString();
   }
   return result;
 }
 @Override
 public String toString() {
   String elementToString = element.toString();
   return new StringBuilder(elementToString.length() + 2)
       .append('[')
       .append(elementToString)
       .append(']')
       .toString();
 }
 /**
  * Returns a String representation of this list.
  *
  * <p>O(n)
  */
 public String toString() {
   StringBuilder result = new StringBuilder(size() + 2);
   result.append("[");
   for (E element : this) {
     result.append(element.toString() + ", ");
   }
   result.append("]");
   return result.toString();
 }
Exemple #11
0
 /* Get the String a string in format "A,B,C" from EnumSet */
 public static <E extends Enum<E>> String getStringFromEnumSet(EnumSet<E> e) {
   String res = "";
   for (E cf : e) {
     res = res + cf.toString() + ",";
   }
   if (res.length() != 0) {
     res = res.substring(0, res.length() - 1);
   }
   return res;
 }
Exemple #12
0
 public void setItem(E object) {
   if (object != null) {
     this.object = object;
     this.item = object;
     this.setText(item.toString());
     this.object = null;
   } else {
     this.item = object;
     this.setText("");
   }
 }
Exemple #13
0
 /**
  * Case insensitive alternative to {@link Enum#valueOf(Class, String)}.
  *
  * @param <E> the concrete Enum type
  * @param enumValues the array of all Enum constants in question, usually per Enum.values()
  * @param constant the constant to get the enum value of
  * @throws IllegalArgumentException if the given constant is not found in the given array of enum
  *     values. Use {@link #containsConstant(Enum[], String)} as a guard to avoid this exception.
  */
 public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant) {
   for (E candidate : enumValues) {
     if (candidate.toString().equalsIgnoreCase(constant)) {
       return candidate;
     }
   }
   throw new IllegalArgumentException(
       String.format(
           "constant [%s] does not exist in enum type %s",
           constant, enumValues.getClass().getComponentType().getName()));
 }
Exemple #14
0
  // 関連付いているListを検索し、引数のNameと一致するListオブジェクトを返す
  public LinkedList<E> find(E name) throws ObjectNotFoundException {

    LinkedList<E> index = this;

    do {
      if (name.equals(index.element)) return index;
      index = index.next;
    } while (index != null);

    // 見つからなかったら、例外をスローする
    throw new ObjectNotFoundException(name.toString());
  }
  @Override
  public Node<E> find(E src) throws ObjectNotFountException {

    Node<E> target = null;
    for (int i = 0; i < this.size; i++) {
      if (src == this.nodes[i].getValue()) {
        target = this.nodes[i];
      }
    }

    if (target == null) {
      throw new ObjectNotFountException("Target: " + src.toString());
    }
    return target;
  }
  @Override
  public void comply(final E object) throws Exception {
    if (object == null) return;
    final int integerPartLength;
    final int fractionPartLength;
    try {
      final BigDecimal bigNum;
      if (object instanceof BigDecimal) bigNum = (BigDecimal) object;
      else bigNum = new BigDecimal(object.toString()).stripTrailingZeros();

      integerPartLength = bigNum.precision() - bigNum.scale();
      fractionPartLength = bigNum.scale() < 0 ? 0 : bigNum.scale();
    } catch (Exception ex) {
      throw ex;
    }

    if (maxIntegerLength < integerPartLength || maxFractionLength < fractionPartLength) throw ex;
  }
Exemple #17
0
  public static <V, E> void write(DirectedGraph<V, E> g, String fileName, EdgeFilter<E> filter)
      throws FileNotFoundException {
    PrintWriter out = new PrintWriter(fileName);

    // System.out.println("Writing '" + fileName + "'");

    out.print("digraph \"DirectedGraph\" { \n graph [label=\"");
    out.print(g.toString());
    out.print("\", labelloc=t, concentrate=true]; ");
    out.print("center=true;fontsize=12;node [fontsize=12];edge [fontsize=12]; \n");

    for (V node : g.vertexSet()) {
      out.print("   \"");
      out.print(getId(node));
      out.print("\" ");
      out.print("[label=\"");
      out.print(node.toString());
      out.print("\" shape=\"box\" color=\"blue\" ] \n");
    }

    for (V src : g.vertexSet()) {
      for (E e : g.outgoingEdgesOf(src)) {
        if (!filter.accept(e)) {
          continue;
        }

        V tgt = g.getEdgeTarget(e);

        out.print(" \"");
        out.print(getId(src));
        out.print("\" -> \"");
        out.print(getId(tgt));
        out.print("\" ");
        out.print("[label=\"");
        out.print(e.toString());
        out.print("\"]\n");
      }
    }

    out.print("\n}");

    out.flush();
    out.close();
  }
Exemple #18
0
  /**
   * Add <tt>element</tt> to the tree.
   *
   * @param parent parent of <tt>node</tt>
   * @param node root of subtree to which element is to be added
   * @param element the element to be added to the tree
   * @throws SearchTreeException if node is found in the tree
   */
  protected BSTNode<E> add(BSTNode<E> parent, BSTNode<E> node, E element) {
    if (node == null) { // base case
      node = new AVLNode(element);
      node.parent = parent;
    } else { // recursive case
      int compareResult = element.compareTo(node.getElement());
      if (compareResult < 0) {
        node.leftChild = add(node, node.leftChild, element);
      } else if (compareResult > 0) {
        node.rightChild = add(node, node.rightChild, element);
      } else {
        throw new SearchTreeException("Duplicate element: " + element.toString());
      }

      // now do height/balance updates and possible
      //  subtree fixes
      node = fixSubtreeRootedAt((AVLNode<E>) node);
    }
    return node;
  }
 @Override
 public void write(@NotNull Bytes bytes, @Nullable E e) {
   bytes.writeUTFΔ(e == null ? null : e.toString());
 }
 private <E> void printList(List<E> list) {
   for (E item : list) {
     System.out.println(item.toString());
   }
 }
 public String toString() {
   return data.toString();
 }
 public String toString() {
   String str = element.toString();
   return str.length() + 2 + '[' + str + ']';
 }
 /**
  * Makes a String representation of the specified ListNode
  *
  * @return String representation of the ListNode
  */
 public String toString() {
   String node = " ";
   node += item.toString();
   return node;
 }
 private String toStringIndex(int index) {
   E value = backingArray[index];
   return value == null ? "null" : value.toString();
 }
Exemple #25
0
  public <ArrayTemplate extends AbstractArrayTemplate<E>, E> void testArray(
      Class<ArrayTemplate> templateClass, ArrayDataSchema schema, List<E> input, List<E> adds)
      throws InstantiationException, IllegalAccessException {
    // out.println("Testing " + templateClass.getName() + " " + schema.toString());

    // constructors and addAll
    ArrayTemplate array1 = templateClass.newInstance();
    array1.addAll(input);
    assertEquals(input, array1);

    /*
    Constructor[] constructors = templateClass.getConstructors();
    for (Constructor c : constructors)
    {
      out.println(c);
    }
    */
    try {
      int size = input.size();

      // constructor(int capacity)
      Constructor<ArrayTemplate> capacityConstructor = templateClass.getConstructor(int.class);
      ArrayTemplate array = capacityConstructor.newInstance(input.size());
      assertEquals(array, Collections.emptyList());
      array.addAll(input);
      assertEquals(input, array);
      array.clear();
      assertEquals(size, input.size());

      // constructor(Collection<E>)
      Constructor<ArrayTemplate> collectionConstructor =
          templateClass.getConstructor(Collection.class);
      array = collectionConstructor.newInstance(input);
      assertEquals(input, array);
      array.clear();
      assertEquals(size, input.size());

      // constructor(DataList)
      Constructor<ArrayTemplate> dataListConstructor = templateClass.getConstructor(DataList.class);
      array = dataListConstructor.newInstance(array1.data());
      assertEquals(array1, array);
      assertEquals(input, array);
      array.clear();
      assertEquals(array1, array);
    } catch (Exception e) {
      assertSame(e, null);
    }

    // test wrapping
    array1.clear();
    array1.addAll(input);
    DataList dataList2 = new DataList();
    ArrayTemplate array2 =
        DataTemplateUtil.wrap(dataList2, schema, templateClass); // with schema arg
    for (E e : input) {
      if (e instanceof DataTemplate) {
        dataList2.add(((DataTemplate<?>) e).data());
      } else if (e instanceof Enum) {
        dataList2.add(e.toString());
      } else {
        dataList2.add(e);
      }
    }
    assertEquals(array1, array2);
    ArrayTemplate array2a = DataTemplateUtil.wrap(dataList2, templateClass); // without schema arg
    assertEquals(array1, array2a);
    assertSame(array2.data(), array2a.data());

    // schema()
    ArrayDataSchema schema1 = array1.schema();
    assertTrue(schema1 != null);
    assertEquals(schema1.getType(), DataSchema.Type.ARRAY);
    assertEquals(schema1, schema);

    // add(E element), get(int index)
    ArrayTemplate array3 = templateClass.newInstance();
    for (int i = 0; i < adds.size(); ++i) {
      E value = adds.get(i);
      assertTrue(array3.add(value));
      assertEquals(array3.get(i), value);
      assertSame(array3.get(i), value);
      assertTrue(array3.toString().contains(value.toString()));
    }
    assertEquals(array3, adds);

    // add(int index, E element), get(int index)
    ArrayTemplate array4 = templateClass.newInstance();
    for (int i = 0; i < adds.size(); ++i) {
      E value = adds.get(adds.size() - i - 1);
      array4.add(0, value);
      assertEquals(array4.get(0), value);
      assertSame(array4.get(0), value);
    }
    assertEquals(array4, adds);

    // clear(), isEmpty(), size()
    assertEquals(array4.size(), adds.size());
    assertFalse(array4.isEmpty());
    array4.clear();
    assertTrue(array4.isEmpty());
    assertEquals(array4.size(), 0);

    // equals()
    array4.clear();
    array4.addAll(input);
    assertTrue(array4.equals(array4));
    assertTrue(array4.equals(input));
    assertFalse(array4.equals(null));
    assertFalse(array4.equals(adds));
    for (int i = 0; i <= input.size(); ++i) {
      List<E> subList = input.subList(0, i);
      ArrayTemplate a = templateClass.newInstance();
      a.addAll(subList);
      if (i == input.size()) {
        assertTrue(array4.equals(subList));
        assertTrue(array4.equals(a));
      } else {
        assertFalse(array4.equals(subList));
        assertFalse(array4.equals(a));
      }
    }

    // hashcode()
    ArrayTemplate array5 = templateClass.newInstance();
    array5.addAll(input);
    assertEquals(array5.hashCode(), array5.data().hashCode());
    array5.addAll(adds);
    assertEquals(array5.hashCode(), array5.data().hashCode());
    array5.clear();
    int lastHash = 0;
    for (int i = 0; i < input.size(); ++i) {
      array5.add(input.get(i));
      int newHash = array5.hashCode();
      if (i > 0) {
        assertFalse(newHash == lastHash);
      }
      lastHash = newHash;
    }

    // indexOf(Object o), lastIndexOf(Object o)
    ArrayTemplate array6 = templateClass.newInstance();
    array6.addAll(adds);
    for (E e : adds) {
      assertEquals(array6.indexOf(e), adds.indexOf(e));
      assertEquals(array6.lastIndexOf(e), adds.lastIndexOf(e));
    }

    // remove(int index), subList(int fromIndex, int toIndex)
    ArrayTemplate array7 = templateClass.newInstance();
    array7.addAll(input);
    ArrayTemplate array8 = templateClass.newInstance();
    array8.addAll(input);
    for (int i = 0; i < input.size(); ++i) {
      array7.remove(0);
      assertEquals(array7, input.subList(i + 1, input.size()));
      assertEquals(array7, array8.subList(i + 1, input.size()));
    }

    // removeRange(int fromIndex, int toIndex), subList(int fromIndex, int toIndex)
    for (int from = 0; from < input.size(); ++from) {
      for (int to = from + 1; to <= input.size(); ++to) {
        ArrayTemplate arrayRemove = templateClass.newInstance();
        arrayRemove.addAll(input);
        InternalList<E> reference = new InternalList<E>(input);
        arrayRemove.removeRange(from, to);
        reference.removeRange(from, to);
        assertEquals(reference, arrayRemove);
      }
    }

    // set(int index, E element)
    ArrayTemplate array9 = templateClass.newInstance();
    array9.addAll(input);
    InternalList<E> reference9 = new InternalList<E>(input);
    for (int i = 0; i < input.size() / 2; ++i) {
      int k = input.size() - i - 1;
      E lo = array9.get(i);
      E hi = array9.get(k);
      E hiPrev = array9.set(k, lo);
      E loPrev = array9.set(i, hi);
      E refHiPrev = reference9.set(k, lo);
      E refLoPrev = reference9.set(i, hi);
      assertEquals(hiPrev, refHiPrev);
      assertEquals(loPrev, refLoPrev);
      assertEquals(array9.get(i), reference9.get(i));
      assertEquals(array9.get(k), reference9.get(k));
    }

    // clone
    Exception exc = null;
    ArrayTemplate array10 = templateClass.newInstance();
    array10.addAll(input);
    try {
      exc = null;
      @SuppressWarnings("unchecked")
      ArrayTemplate array10Clone = (ArrayTemplate) array10.clone();
      assertEquals(array10Clone, array10);
      assertTrue(array10Clone != array10);
      array10Clone.remove(0);
      assertEquals(array10Clone.size(), array10.size() - 1);
      assertFalse(array10Clone.equals(array10));
      assertTrue(array10.containsAll(array10Clone));
      array10.remove(0);
      assertEquals(array10Clone, array10);
    } catch (CloneNotSupportedException e) {
      exc = e;
    }
    assert (exc == null);

    // contains
    for (int i = 0; i < input.size(); ++i) {
      ArrayTemplate array = templateClass.newInstance();
      E v = input.get(i);
      array.add(v);
      for (int k = 0; k < input.size(); ++k) {
        if (k == i) assertTrue(array.contains(v));
        else assertFalse(array.contains(input.get(k)));
      }
    }

    // containsAll
    ArrayTemplate arrayContainsAll = templateClass.newInstance();
    arrayContainsAll.addAll(input);
    arrayContainsAll.addAll(adds);
    InternalList<E> referenceContainsAll = new InternalList<E>(input);
    referenceContainsAll.addAll(adds);
    for (int from = 0; from < arrayContainsAll.size(); ++from) {
      for (int to = from + 1; to <= arrayContainsAll.size(); ++to) {
        boolean testResult = arrayContainsAll.containsAll(referenceContainsAll.subList(from, to));
        boolean referenceResult =
            referenceContainsAll.containsAll(referenceContainsAll.subList(from, to));
        assertEquals(testResult, referenceResult);
        assertTrue(testResult);
        assertTrue(referenceResult);
      }
      boolean testResult2 =
          arrayContainsAll.subList(from, arrayContainsAll.size()).containsAll(referenceContainsAll);
      boolean referenceResult2 =
          referenceContainsAll
              .subList(from, arrayContainsAll.size())
              .containsAll(referenceContainsAll);
      // out.println("from " + from + " test " + testResult2 + " ref " + referenceResult2);
      assertEquals(testResult2, referenceResult2);
    }

    // removeAll
    InternalList<E> referenceListRemoveAll = new InternalList<E>(input);
    referenceListRemoveAll.addAll(adds);
    for (int from = 0; from < referenceListRemoveAll.size(); ++from) {
      for (int to = from + 1; to <= referenceListRemoveAll.size(); ++to) {
        ArrayTemplate arrayRemoveAll = templateClass.newInstance();
        arrayRemoveAll.addAll(referenceListRemoveAll);
        InternalList<E> referenceRemoveAll = new InternalList<E>(referenceListRemoveAll);

        boolean testResult = arrayRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
        boolean referenceResult =
            referenceRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
        // out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRemoveAll +
        // " ref " + referenceResult + " " + referenceRemoveAll);
        assertEquals(arrayRemoveAll, referenceRemoveAll);
        assertEquals(testResult, referenceResult);
        assertTrue(testResult);
        assertTrue(referenceResult);
      }
    }

    // retainAll
    InternalList<E> referenceListRetainAll = new InternalList<E>(input);
    referenceListRetainAll.addAll(adds);
    for (int from = 0; from < referenceListRetainAll.size(); ++from) {
      for (int to = from + 1; to <= referenceListRetainAll.size(); ++to) {
        ArrayTemplate arrayRetainAll = templateClass.newInstance();
        arrayRetainAll.addAll(referenceListRetainAll);
        InternalList<E> referenceRetainAll = new InternalList<E>(referenceListRetainAll);

        boolean testResult = arrayRetainAll.removeAll(referenceListRetainAll.subList(from, to));
        boolean referenceResult =
            referenceRetainAll.removeAll(referenceListRetainAll.subList(from, to));
        // out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRetainAll +
        // " ref " + referenceResult + " " + referenceRetainAll);
        assertEquals(arrayRetainAll, referenceRetainAll);
        assertEquals(testResult, referenceResult);
        assertTrue(testResult);
        assertTrue(referenceResult);
      }
    }

    // Iterator
    ArrayTemplate arrayIt = templateClass.newInstance();
    arrayIt.addAll(input);
    arrayIt.addAll(adds);
    for (Iterator<E> it = arrayIt.iterator(); it.hasNext(); ) {
      it.next();
      it.remove();
    }
    assertTrue(arrayIt.isEmpty());

    // ListIterator hasNext, hasPrevious, next, previous
    ArrayTemplate arrayListIt = templateClass.newInstance();
    arrayListIt.addAll(input);
    arrayListIt.addAll(adds);
    for (int i = 0; i <= arrayListIt.size(); ++i) {
      ListIterator<E> it = arrayListIt.listIterator(i);
      if (i > 0) {
        int save = it.nextIndex();
        assertTrue(it.hasPrevious());
        assertEquals(it.previous(), arrayListIt.get(i - 1));
        it.next();
        assertEquals(it.nextIndex(), save);
      } else {
        assertFalse(it.hasPrevious());
      }
      if (i < arrayListIt.size()) {
        int save = it.previousIndex();
        assertTrue(it.hasNext());
        assertEquals(it.next(), arrayListIt.get(i));
        it.previous();
        assertEquals(it.previousIndex(), save);
      } else {
        assertFalse(it.hasNext());
      }
      assertEquals(it.nextIndex(), i);
      assertEquals(it.previousIndex(), i - 1);
    }

    // ListIterator remove
    for (ListIterator<E> it = arrayListIt.listIterator(); it.hasNext(); ) {
      it.next();
      it.remove();
    }
    assertTrue(arrayListIt.isEmpty());

    // ListIterator add
    arrayListIt.clear();
    {
      ListIterator<E> it = arrayListIt.listIterator();
      for (E e : adds) {
        it.add(e);
      }
    }
    assertEquals(arrayListIt, adds);

    // ListIterator set
    for (int i = 0; i < adds.size(); ++i) {
      ListIterator<E> it = arrayListIt.listIterator(i);
      it.next();
      E value = adds.get(adds.size() - i - 1);
      it.set(value);
    }
    for (int i = 0; i < adds.size(); ++i) {
      E value = adds.get(adds.size() - i - 1);
      assertEquals(arrayListIt.get(i), value);
    }
  }
 public java.lang.String toString() {
   E v = get();
   return v == null ? null : v.toString();
 }
 @Override
 public String toString() {
   return '[' + element.toString() + ']';
 }
Exemple #28
0
 @Override
 public String toString() {
   return "score: " + score.total() + ", item: " + item.toString();
 }
 public String stringize(final E element) {
   return element != null ? element.toString() : "null";
 }
Exemple #30
0
 @Override
 public String toString() {
   return left.toString() + ":" + mid.toString() + ":" + right.toString();
 }