Esempio n. 1
0
  /**
   * Create a new instance of SortedArray.<br>
   * If the comparator is {@code null}, the type of class must be a {@link Comparable} to use the
   * "natural order"
   *
   * @param typeClass Type of element
   * @param comparator Comparator to use
   * @param initialSize Initial array capacity
   * @param unique Indicates if the unique mode is enable
   */
  @SuppressWarnings("unchecked")
  public SortedArray(
      final Class<TYPE> typeClass,
      final Comparator<TYPE> comparator,
      final int initialSize,
      final boolean unique) {
    if (typeClass == null) {
      throw new NullPointerException("typeClass musn't be null");
    }

    this.comparator = comparator;

    if (comparator == null) {
      if (Reflector.isSubTypeOf(typeClass, Comparable.class) == true) {
        this.comparator = new ComparatorNatural<TYPE>();
      } else {
        throw new IllegalArgumentException(
            "comparator is null and the type class " + typeClass.getName() + " is not comparable");
      }
    }

    this.typeClass = typeClass;
    this.array = (TYPE[]) Array.newInstance(typeClass, Math.max(128, initialSize));

    this.unique = unique;
    this.size = 0;
  }