Beispiel #1
0
 public static <T> boolean isInSet(T value, T... set) {
   for (T i : set) {
     if (i.equals(value)) {
       return true;
     }
   }
   return false;
 }
 public static <T> void assertOneOf(T value, T... values) {
   boolean found = false;
   for (T v : values) {
     if (value == v || value != null && value.equals(v)) {
       found = true;
     }
   }
   Assert.assertTrue(value + " should be equal to one of " + Arrays.toString(values), found);
 }
Beispiel #3
0
 public int lastIndexOfFirst(T t, int index) {
   try {
     for (int i = index; i >= 0; i--) {
       if ((t == null ? get(i).first == null : t.equals(get(i).first))) return i;
     }
   } catch (final Exception e) {
   }
   return -1;
 }
Beispiel #4
0
 public synchronized boolean removeFirst(T t) {
   Quint<T, K, L, M, N> pair;
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     pair = i.next();
     if ((t == null ? pair.first == null : t.equals(pair.first))) {
       i.remove();
       return true;
     }
   }
   return false;
 }
Beispiel #5
0
 @Override
 public Vector<T> removeAll(T element) {
   HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty();
   for (int i = 0; i < length(); i++) {
     final T value = get(i);
     if (!element.equals(value)) {
       result = result.put(result.size(), value);
     }
   }
   return result.size() == length() ? this : (result.isEmpty() ? empty() : new Vector<>(result));
 }
 /**
  * Appends the given element to the list only if the element is not yet contained in the given
  * list.
  *
  * @param list The list of elements.
  * @param element The element to append.
  * @param <T> The type of the list.
  * @return A list with the appended element.
  */
 public static <T> List<T> joinUnique(List<? extends T> list, T element) {
   List<T> result = new ArrayList<T>(list.size() + 1);
   for (T listElement : list) {
     if (listElement.equals(element)) {
       throw new IllegalArgumentException(
           "Conflicting elements: " + listElement + " and " + element);
     }
     result.add(listElement);
   }
   result.add(element);
   return result;
 }
Beispiel #7
0
  /**
   * Checks equality of two objects based on the equality of their fields, items, or .equals()
   * method.
   *
   * @param obj1
   * @param obj2
   * @return
   */
  public static <T> boolean equal(T obj1, T obj2) {
    if (obj1 == null || obj2 == null) {
      // If they're both null, we call this equal
      if (obj1 == null && obj2 == null) return true;
      else return false;
    }

    if (!obj1.getClass().equals(obj2.getClass())) return false;

    if (obj1.equals(obj2)) return true;

    List<Pair> vals = new ArrayList<Pair>();

    // If obj1 and obj2 are Collections, get the objects in them
    if (Collection.class.isAssignableFrom(obj1.getClass())) {

      Collection c1 = (Collection) obj1;
      Collection c2 = (Collection) obj2;

      if (c1.size() != c2.size()) return false;

      Iterator itr1 = c1.iterator();
      Iterator itr2 = c2.iterator();

      while (itr1.hasNext() && itr2.hasNext()) {
        vals.add(new Pair(itr1.next(), itr2.next()));
      }
    }

    // Get field values from obj1 and obj2
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(obj1);
    for (PropertyDescriptor property : properties) {

      // ignore getClass() and isEmpty()
      if (property.getName().equals("class") || property.getName().equals("empty")) continue;

      Object val1 = invokeMethod(obj1, property.getReadMethod(), null, property.getName());
      Object val2 = invokeMethod(obj2, property.getReadMethod(), null, property.getName());

      vals.add(new Pair(val1, val2));
    }

    if (vals.isEmpty()) return false;

    for (Pair pair : vals) {
      if (!equal(pair.left, pair.right)) return false;
    }

    return true;
  }
Beispiel #8
0
 @Override
 public Array<T> replaceAll(T currentElement, T newElement) {
   final Object[] arr = new Object[length()];
   boolean changed = false;
   for (int i = 0; i < length(); i++) {
     final T value = get(i);
     if (currentElement.equals(value)) {
       arr[i] = newElement;
       changed = true;
     } else {
       arr[i] = get(i);
     }
   }
   return changed ? wrap(arr) : this;
 }
Beispiel #9
0
 @Override
 public Vector<T> replaceAll(T currentElement, T newElement) {
   HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty();
   boolean changed = false;
   for (int i = 0; i < length(); i++) {
     final T value = get(i);
     if (currentElement.equals(value)) {
       trie = trie.put(trie.size(), newElement);
       changed = true;
     } else {
       trie = trie.put(trie.size(), value);
     }
   }
   return changed ? (trie.isEmpty() ? empty() : new Vector<>(trie)) : this;
 }
Beispiel #10
0
 @Override
 public Array<T> remove(T element) {
   int index = -1;
   for (int i = 0; i < length(); i++) {
     final T value = get(i);
     if (element.equals(value)) {
       index = i;
       break;
     }
   }
   if (index < 0) {
     return this;
   } else {
     return removeAt(index);
   }
 }
Beispiel #11
0
 @Override
 public Vector<T> remove(T element) {
   HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty();
   boolean found = false;
   for (int i = 0; i < length(); i++) {
     final T value = get(i);
     if (found) {
       trie = trie.put(trie.size(), value);
     } else {
       if (element.equals(value)) {
         found = true;
       } else {
         trie = trie.put(trie.size(), value);
       }
     }
   }
   return trie.size() == length() ? this : trie.isEmpty() ? empty() : new Vector<>(trie);
 }
Beispiel #12
0
 @Override
 public boolean equals(Object obj) {
   if (this == obj) {
     return true;
   }
   if (obj == null) {
     return false;
   }
   if (getClass() != obj.getClass()) {
     return false;
   }
   GenericTreeNode<?> other = (GenericTreeNode<?>) obj;
   if (data == null) {
     if (other.data != null) {
       return false;
     }
   } else if (!data.equals(other.data)) {
     return false;
   }
   return true;
 }
 /**
  * Annotates a map with any difference encountered in a simple value report argument that differs
  * between this an another {@link RecalibrationArgumentCollection} instance.
  *
  * <p>The key of the new entry would be the name of that argument in the report file. The value is
  * a message that explains the difference to the end user.
  *
  * <p>
  *
  * <p>This method should not return any exception.
  *
  * @param diffs where to annotate the differences.
  * @param name the name of the report argument to compare.
  * @param thisValue this argument collection value for that argument.
  * @param otherValue the other collection value for that argument.
  * @param thisRole the name used to refer to this RAC report that makes sense to the end user.
  * @param otherRole the name used to refer to the other RAC report that makes sense to the end
  *     user.
  * @type T the argument Object value type.
  * @return <code>true</code> if a difference has been spotted, thus <code>diff</code> has been
  *     modified.
  */
 private <T> boolean compareSimpleReportArgument(
     final Map<String, String> diffs,
     final String name,
     final T thisValue,
     final T otherValue,
     final String thisRole,
     final String otherRole) {
   if (thisValue == null && otherValue == null) {
     return false;
   } else if (thisValue != null && thisValue.equals(otherValue)) {
     return false;
   } else {
     diffs.put(
         name,
         String.format(
             "differences between '%s' {%s} and '%s' {%s}.",
             thisRole,
             thisValue == null ? "" : thisValue,
             otherRole,
             otherValue == null ? "" : otherValue));
     return true;
   }
 }
Beispiel #14
0
 public boolean containsFirst(T t) {
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     if ((t == null) ? i.next() == null : t.equals(i.next().first)) return true;
   }
   return false;
 }
 @UsedFromByteCode
 public static <T> boolean notEquals(T t1, T t2) {
   return t1 == null ? t2 != null : !t1.equals(t2);
 }
Beispiel #16
0
 public static <T> boolean areObjectsEqual(T a, T b) {
   if (a == null) {
     return b == null;
   }
   return a.equals(b);
 }