poo en JAVA

poo en JAVA - Java - Programmation

Marsh Posté le 02-05-2009 à 11:25:42    

j'ai un problem a propo d'une creation d'un  objet de classe en java  
je vous demande de m'envoyer un code en java pour creer une ligne et et cercle
merci

Reply

Marsh Posté le 02-05-2009 à 11:25:42   

Reply

Marsh Posté le 02-05-2009 à 12:21:32    

Code :
  1. /*
  2. * Copyright (C) 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License" );
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15.  
  16. package com.google.common.collect;
  17.  
  18. import com.google.common.annotations.GwtCompatible;
  19. import static com.google.common.base.Preconditions.checkNotNull;
  20.  
  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import java.util.Set;
  30.  
  31. import javax.annotation.Nullable;
  32.  
  33. /**
  34. * A high-performance, immutable {@code Set} with reliable, user-specified
  35. * iteration order. Does not permit null elements.
  36. *
  37. * <p>Unlike {@link Collections#unmodifiableSet}, which is a <i>view</i> of a
  38. * separate collection that can still change, an instance of this class contains
  39. * its own private data and will <i>never</i> change. This class is convenient
  40. * for {@code public static final} sets ("constant sets" ) and also lets you
  41. * easily make a "defensive copy" of a set provided to your class by a caller.
  42. *
  43. * <p><b>Warning:</b> Like most sets, an {@code ImmutableSet} will not function
  44. * correctly if an element is modified after being placed in the set. For this
  45. * reason, and to avoid general confusion, it is strongly recommended to place
  46. * only immutable objects into this collection.
  47. *
  48. * <p>This class has been observed to perform significantly better than {@link
  49. * HashSet} for objects with very fast {@link Object#hashCode} implementations
  50. * (as a well-behaved immutable object should). While this class's factory
  51. * methods create hash-based instances, the {@link ImmutableSortedSet} subclass
  52. * performs binary searches instead.
  53. *
  54. * <p><b>Note</b>: Although this class is not final, it cannot be subclassed
  55. * outside its package as it has no public or protected constructors. Thus,
  56. * instances of this type are guaranteed to be immutable.
  57. *
  58. * @see ImmutableList
  59. * @see ImmutableMap
  60. * @author Kevin Bourrillion
  61. * @author Nick Kralevich
  62. */
  63. @GwtCompatible(serializable = true)
  64. @SuppressWarnings("serial" ) // we're overriding default serialization
  65. public abstract class ImmutableSet<E> extends ImmutableCollection<E>
  66.    implements Set<E> {
  67.  private static final ImmutableSet<?> EMPTY_IMMUTABLE_SET
  68.      = new EmptyImmutableSet();
  69.  
  70.  /**
  71.   * Returns the empty immutable set. This set behaves and performs comparably
  72.   * to {@link Collections#emptySet}, and is preferable mainly for consistency
  73.   * and maintainability of your code.
  74.   */
  75.  // Casting to any type is safe because the set will never hold any elements.
  76.  @SuppressWarnings({"unchecked"})
  77.  public static <E> ImmutableSet<E> of() {
  78.    return (ImmutableSet<E> ) EMPTY_IMMUTABLE_SET;
  79.  }
  80.  
  81.  /**
  82.   * Returns an immutable set containing a single element. This set behaves and
  83.   * performs comparably to {@link Collections#singleton}, but will not accept
  84.   * a null element. It is preferable mainly for consistency and
  85.   * maintainability of your code.
  86.   */
  87.  public static <E> ImmutableSet<E> of(E element) {
  88.    return new SingletonImmutableSet<E>(element, element.hashCode());
  89.  }
  90.  
  91.  /**
  92.   * Returns an immutable set containing the given elements, in order. Repeated
  93.   * occurrences of an element (according to {@link Object#equals}) after the
  94.   * first are ignored (but too many of these may result in the set being
  95.   * sized inappropriately).
  96.   *
  97.   * @throws NullPointerException if any of {@code elements} is null
  98.   */
  99.  public static <E> ImmutableSet<E> of(E... elements) {
  100.    switch (elements.length) {
  101.      case 0:
  102.        return of();
  103.      case 1:
  104.        return of(elements[0]);
  105.      default:
  106.        return create(Arrays.asList(elements), elements.length);
  107.    }
  108.  }
  109.  
  110.  /**
  111.   * Returns an immutable set containing the given elements, in order. Repeated
  112.   * occurrences of an element (according to {@link Object#equals}) after the
  113.   * first are ignored (but too many of these may result in the set being
  114.   * sized inappropriately). This method iterates over {@code elements} at most
  115.   * once.
  116.   *
  117.   * <p>Note that if {@code s} is a {@code Set<String>}, then {@code
  118.   * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing
  119.   * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns
  120.   * a {@code ImmutableSet<Set<String>>} containing one element (the given set
  121.   * itself).
  122.   *
  123.   * <p><b>Note:</b> Despite what the method name suggests, if {@code elements}
  124.   * is an {@code ImmutableSet} (but not an {@code ImmutableSortedSet}), no copy
  125.   * will actually be performed, and the given set itself will be returned.
  126.   *
  127.   * @throws NullPointerException if any of {@code elements} is null
  128.   */
  129.  public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) {
  130.    if (elements instanceof ImmutableSet
  131.        && !(elements instanceof ImmutableSortedSet)) {
  132.      @SuppressWarnings("unchecked" ) // all supported methods are covariant
  133.      ImmutableSet<E> set = (ImmutableSet<E> ) elements;
  134.      return set;
  135.    }
  136.    return copyOfInternal(Collections2.toCollection(elements));
  137.  }
  138.  
  139.  /**
  140.   * Returns an immutable set containing the given elements, in order. Repeated
  141.   * occurrences of an element (according to {@link Object#equals}) after the
  142.   * first are ignored.
  143.   *
  144.   * @throws NullPointerException if any of {@code elements} is null
  145.   */
  146.  public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) {
  147.    Collection<E> list = Lists.newArrayList(elements);
  148.    return copyOfInternal(list);
  149.  }
  150.  
  151.  private static <E> ImmutableSet<E> copyOfInternal(
  152.      Collection<? extends E> collection) {
  153.    // TODO: Support concurrent collections that change while this method is
  154.    // running.
  155.    switch (collection.size()) {
  156.      case 0:
  157.        return of();
  158.      case 1:
  159.        // TODO: Remove "ImmutableSet.<E>" when eclipse bug is fixed.
  160.        return ImmutableSet.<E>of(collection.iterator().next());
  161.      default:
  162.        return create(collection, collection.size());
  163.    }
  164.  }
  165.  
  166.  ImmutableSet() {}
  167.  
  168.  /** Returns {@code true} if the {@code hashCode()} method runs quickly. */
  169.  boolean isHashCodeFast() {
  170.    return false;
  171.  }
  172.  
  173.  @Override public boolean equals(@Nullable Object object) {
  174.    if (object == this) {
  175.      return true;
  176.    }
  177.    if (object instanceof ImmutableSet
  178.        && isHashCodeFast()
  179.        && ((ImmutableSet<?> ) object).isHashCodeFast()
  180.        && hashCode() != object.hashCode()) {
  181.      return false;
  182.    }
  183.    return Collections2.setEquals(this, object);
  184.  }
  185.  
  186.  @Override public int hashCode() {
  187.    int hashCode = 0;
  188.    for (Object o : this) {
  189.      hashCode += o.hashCode();
  190.    }
  191.    return hashCode;
  192.  }
  193.  
  194.  // This declaration is needed to make Set.iterator() and
  195.  // ImmutableCollection.iterator() consistent.
  196.  @Override public abstract UnmodifiableIterator<E> iterator();
  197.  
  198.  // Package-private for GWT serialization.
  199.  static final class EmptyImmutableSet extends ImmutableSet<Object> {
  200.    public int size() {
  201.      return 0;
  202.    }
  203.  
  204.    @Override public boolean isEmpty() {
  205.      return true;
  206.    }
  207.  
  208.    @Override public boolean contains(Object target) {
  209.      return false;
  210.    }
  211.  
  212.    @Override public UnmodifiableIterator<Object> iterator() {
  213.      return Iterators.emptyIterator();
  214.    }
  215.  
  216.    private static final Object[] EMPTY_ARRAY = new Object[0];
  217.  
  218.    @Override public Object[] toArray() {
  219.      return EMPTY_ARRAY;
  220.    }
  221.  
  222.    @Override public <T> T[] toArray(T[] a) {
  223.      if (a.length > 0) {
  224.        a[0] = null;
  225.      }
  226.      return a;
  227.    }
  228.  
  229.    @Override public boolean containsAll(Collection<?> targets) {
  230.      return targets.isEmpty();
  231.    }
  232.  
  233.    @Override public boolean equals(@Nullable Object object) {
  234.      if (object instanceof Set) {
  235.        Set<?> that = (Set<?> ) object;
  236.        return that.isEmpty();
  237.      }
  238.      return false;
  239.    }
  240.  
  241.    @Override public final int hashCode() {
  242.      return 0;
  243.    }
  244.  
  245.    @Override boolean isHashCodeFast() {
  246.      return true;
  247.    }
  248.  
  249.    @Override public String toString() {
  250.      return "[]";
  251.    }
  252.  }
  253.  
  254.  private static <E> ImmutableSet<E> create(
  255.      Iterable<? extends E> iterable, int count) {
  256.    // count is always the (nonzero) number of elements in the iterable
  257.    int tableSize = Hashing.chooseTableSize(count);
  258.    Object[] table = new Object[tableSize];
  259.    int mask = tableSize - 1;
  260.  
  261.    List<E> elements = new ArrayList<E>(count);
  262.    int hashCode = 0;
  263.  
  264.    for (E element : iterable) {
  265.      int hash = element.hashCode();
  266.      for (int i = Hashing.smear(hash); true; i++) {
  267.        int index = i & mask;
  268.        Object value = table[index];
  269.        if (value == null) {
  270.          // Came to an empty bucket. Put the element here.
  271.          table[index] = element;
  272.          elements.add(element);
  273.          hashCode += hash;
  274.          break;
  275.        } else if (value.equals(element)) {
  276.          break; // Found a duplicate. Nothing to do.
  277.        }
  278.      }
  279.    }
  280.  
  281.    // The iterable might have contained only duplicates of the same element.
  282.    return (elements.size() == 1)
  283.        ? new SingletonImmutableSet<E>(elements.get(0), hashCode)
  284.        : new RegularImmutableSet<E>(elements.toArray(), hashCode, table, mask);
  285.  }
  286.  
  287.  abstract static class ArrayImmutableSet<E> extends ImmutableSet<E> {
  288.    final Object[] elements; // the elements (two or more) in the desired order
  289.  
  290.    ArrayImmutableSet(Object[] elements) {
  291.      this.elements = elements;
  292.    }
  293.  
  294.    public int size() {
  295.      return elements.length;
  296.    }
  297.  
  298.    @Override public boolean isEmpty() {
  299.      return false;
  300.    }
  301.  
  302.    /*
  303.     * The cast is safe because the only way to create an instance is via the
  304.     * create() method above, which only permits elements of type E.
  305.     */
  306.    @SuppressWarnings("unchecked" )
  307.    @Override public UnmodifiableIterator<E> iterator() {
  308.      return (UnmodifiableIterator<E> ) Iterators.forArray(elements);
  309.    }
  310.  
  311.    @Override public Object[] toArray() {
  312.      Object[] array = new Object[size()];
  313.      System.arraycopy(elements, 0, array, 0, size());
  314.      return array;
  315.    }
  316.  
  317.    @Override public <T> T[] toArray(T[] array) {
  318.      int size = size();
  319.      if (array.length < size) {
  320.        array = ObjectArrays.newArray(array, size);
  321.      } else if (array.length > size) {
  322.        array[size] = null;
  323.      }
  324.      System.arraycopy(elements, 0, array, 0, size);
  325.      return array;
  326.    }
  327.  
  328.    @Override public boolean containsAll(Collection<?> targets) {
  329.      if (targets == this) {
  330.        return true;
  331.      }
  332.      if (!(targets instanceof ArrayImmutableSet)) {
  333.        return super.containsAll(targets);
  334.      }
  335.      if (targets.size() > size()) {
  336.        return false;
  337.      }
  338.      for (Object target : ((ArrayImmutableSet<?> ) targets).elements) {
  339.        if (!contains(target)) {
  340.          return false;
  341.        }
  342.      }
  343.      return true;
  344.    }
  345.  }
  346.  
  347.  /** such as ImmutableMap.keySet() */
  348.  abstract static class TransformedImmutableSet<D, E> extends ImmutableSet<E> {
  349.    final D[] source;
  350.    final int hashCode;
  351.  
  352.    TransformedImmutableSet(D[] source, int hashCode) {
  353.      this.source = source;
  354.      this.hashCode = hashCode;
  355.    }
  356.  
  357.    abstract E transform(D element);
  358.  
  359.    public int size() {
  360.      return source.length;
  361.    }
  362.  
  363.    @Override public boolean isEmpty() {
  364.      return false;
  365.    }
  366.  
  367.    @Override public UnmodifiableIterator<E> iterator() {
  368.      Iterator<E> iterator = new AbstractIterator<E>() {
  369.        int index = 0;
  370.        @Override protected E computeNext() {
  371.          return index < source.length
  372.              ? transform(source[index++])
  373.              : endOfData();
  374.        }
  375.      };
  376.      // Though the AbstractIterator is unmodifiable, it isn't an
  377.      // UnmodifiableIterator.
  378.      return Iterators.unmodifiableIterator(iterator);
  379.    }
  380.  
  381.    @Override public Object[] toArray() {
  382.      return toArray(new Object[size()]);
  383.    }
  384.  
  385.    @Override public <T> T[] toArray(T[] array) {
  386.      int size = size();
  387.      if (array.length < size) {
  388.        array = ObjectArrays.newArray(array, size);
  389.      } else if (array.length > size) {
  390.        array[size] = null;
  391.      }
  392.  
  393.      for (int i = 0; i < source.length; i++) {
  394.        // Technically unsafe. But if T is generic, the caller already got a
  395.        // warning when they created their array, and if it isn't, we can count
  396.        // on an ArrayStoreException in the following statement to catch any
  397.        // problem.
  398.        @SuppressWarnings("unchecked" )
  399.        T t = (T) transform(source[i]);
  400.        array[i] = t;
  401.      }
  402.      return array;
  403.    }
  404.  
  405.    @Override public final int hashCode() {
  406.      return hashCode;
  407.    }
  408.  
  409.    @Override boolean isHashCodeFast() {
  410.      return true;
  411.    }
  412.  }
  413.  
  414.  /*
  415.   * This class is used to serialize all ImmutableSet instances, regardless of
  416.   * implementation type. It captures their "logical contents" and they are
  417.   * reconstructed using public static factories. This is necessary to ensure
  418.   * that the existence of a particular implementation type is an implementation
  419.   * detail.
  420.   */
  421.  private static class SerializedForm implements Serializable {
  422.    final Object[] elements;
  423.    SerializedForm(Object[] elements) {
  424.      this.elements = elements;
  425.    }
  426.    Object readResolve() {
  427.      return of(elements);
  428.    }
  429.    private static final long serialVersionUID = 0;
  430.  }
  431.  
  432.  @Override Object writeReplace() {
  433.    return new SerializedForm(toArray());
  434.  }
  435.  
  436.  /**
  437.   * Returns a new builder. The generated builder is equivalent to the builder
  438.   * created by the {@link Builder} constructor.
  439.   */
  440.  public static <E> Builder<E> builder() {
  441.    return new Builder<E>();
  442.  }
  443.  
  444.  /**
  445.   * A builder for creating immutable set instances, especially
  446.   * {@code public static final} sets ("constant sets" ).
  447.   *
  448.   * <p>Example:
  449.   * <pre>{@code
  450.   *   public static final ImmutableSet<Color> GOOGLE_COLORS
  451.   *       = new ImmutableSet.Builder<Color>()
  452.   *           .addAll(WEBSAFE_COLORS)
  453.   *           .add(new Color(0, 191, 255))
  454.   *           .build();}</pre>
  455.   *
  456.   * <p>Builder instances can be reused - it is safe to call {@link #build}
  457.   * multiple times to build multiple sets in series. Each set
  458.   * is a superset of the set created before it.
  459.   */
  460.  public static class Builder<E> {
  461.    final ArrayList<E> contents = Lists.newArrayList();
  462.  
  463.    /**
  464.     * Creates a new builder. The returned builder is equivalent to the builder
  465.     * generated by {@link ImmutableSet#builder}.
  466.     */
  467.    public Builder() {}
  468.  
  469.    /**
  470.     * Adds {@code element} to the {@code ImmutableSet}.  If the
  471.     * {@code ImmutableSet} already contains {@code element}, then
  472.     * {@code add} has no effect. (only the previously added element
  473.     * is retained).
  474.     *
  475.     * @param element the element to add
  476.     * @return this {@code Builder} object
  477.     * @throws NullPointerException if {@code element} is null
  478.     */
  479.    public Builder<E> add(E element) {
  480.      checkNotNull(element, "element cannot be null" );
  481.      contents.add(element);
  482.      return this;
  483.    }
  484.  
  485.    /**
  486.     * Adds each element of {@code elements} to the {@code ImmutableSet},
  487.     * ignoring duplicate elements (only the first duplicate element is added).
  488.     *
  489.     * @param elements the elements to add
  490.     * @return this {@code Builder} object
  491.     * @throws NullPointerException if {@code elements} is or contains null
  492.     */
  493.    public Builder<E> add(E... elements) {
  494.      checkNotNull(elements, "elements cannot be null" );
  495.      for (E element : elements) {
  496.        if (element == null) {
  497.          throw new NullPointerException(
  498.              "null element in: " + Arrays.toString(elements));
  499.        }
  500.        contents.add(element);
  501.      }
  502.      return this;
  503.    }
  504.  
  505.    /**
  506.     * Adds each element of {@code elements} to the {@code ImmutableSet},
  507.     * ignoring duplicate elements (only the first duplicate element is added).
  508.     *
  509.     * @param elements the {@code Iterable} to add to the {@code ImmutableSet}
  510.     * @return this {@code Builder} object
  511.     * @throws NullPointerException if {@code elements} is or contains null
  512.     */
  513.    public Builder<E> addAll(Iterable<? extends E> elements) {
  514.      if (elements instanceof Collection) {
  515.        @SuppressWarnings("unchecked" )
  516.        Collection<? extends E> collection = (Collection<? extends E> ) elements;
  517.        contents.ensureCapacity(contents.size() + collection.size());
  518.      }
  519.      for (E elem : elements) {
  520.        checkNotNull(elem, "elements contains a null" );
  521.        contents.add(elem);
  522.      }
  523.      return this;
  524.    }
  525.  
  526.    /**
  527.     * Adds each element of {@code elements} to the {@code ImmutableSet},
  528.     * ignoring duplicate elements (only the first duplicate element is added).
  529.     *
  530.     * @param elements the elements to add to the {@code ImmutableSet}
  531.     * @return this {@code Builder} object
  532.     * @throws NullPointerException if {@code elements} is or contains null
  533.     */
  534.    public Builder<E> addAll(Iterator<? extends E> elements) {
  535.      while (elements.hasNext()) {
  536.        E element = elements.next();
  537.        checkNotNull(element, "element cannot be null" );
  538.        contents.add(element);
  539.      }
  540.      return this;
  541.    }
  542.  
  543.    /**
  544.     * Returns a newly-created {@code ImmutableSet} based on the contents of
  545.     * the {@code Builder}.
  546.     */
  547.    public ImmutableSet<E> build() {
  548.      return copyOf(contents);
  549.    }
  550.  }
  551. }


Message édité par masklinn le 02-05-2009 à 12:22:33

---------------
Stick a parrot in a Call of Duty lobby, and you're gonna get a racist parrot. — Cody
Reply

Marsh Posté le 02-05-2009 à 13:20:06    

Hors charte : on ne fait pas de résolution d'exercices.
 
Fermeture du sujet.

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed