Array.java
0001 /*
0002  * Java Genetic Algorithm Library (jenetics-1.6.0).
0003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
0004  *
0005  * Licensed under the Apache License, Version 2.0 (the "License");
0006  * you may not use this file except in compliance with the License.
0007  * You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  *
0017  * Author:
0018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
0019  */
0020 package org.jenetics.util;
0021 
0022 import static java.lang.Math.min;
0023 import static java.lang.String.format;
0024 import static java.lang.System.arraycopy;
0025 import static java.util.Objects.requireNonNull;
0026 
0027 import java.util.Arrays;
0028 import java.util.Collection;
0029 import java.util.Comparator;
0030 import java.util.Iterator;
0031 import java.util.List;
0032 import java.util.ListIterator;
0033 import java.util.Random;
0034 import java.util.RandomAccess;
0035 
0036 import javolution.context.StackContext;
0037 import javolution.util.FastList;
0038 
0039 /**
0040  * Array class which wraps the the java build in array type T[]. Once the array
0041  * is created the array length can't be changed (like the build in array).
0042  <p/>
0043  <strong>Note that this implementation is not synchronized.</strong> If
0044  * multiple threads access this object concurrently, and at least one of the
0045  * threads modifies it, it must be synchronized externally.
0046  *
0047  @param <T> the element type of the array.
0048  *
0049  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
0050  @since 1.0
0051  @version 1.6 &mdash; <em>$Date: 2014-02-17 $</em>
0052  */
0053 public final class Array<T>
0054     extends ArraySeq<T>
0055     implements
0056         MSeq<T>,
0057         RandomAccess
0058 {
0059     private static final long serialVersionUID = 2L;
0060 
0061     @SuppressWarnings("rawtypes")
0062     private static final Array EMPTY = new Array(0);
0063 
0064     /**
0065      * Return the empty array.
0066      *
0067      @param <T> the element type.
0068      @return empty array.
0069      */
0070     @SuppressWarnings("unchecked")
0071     public static <T> Array<T> empty() {
0072         return EMPTY;
0073     }
0074 
0075     Array(final ArrayRef array, final int start, final int end) {
0076         super(array, start, end);
0077     }
0078 
0079     /**
0080      * Create a new array with the given length.
0081      *
0082      @param length the array length.
0083      @throws NegativeArraySizeException if the specified {@code length}
0084      *          is negative
0085      */
0086     public Array(final int length) {
0087         super(length);
0088     }
0089 
0090     /**
0091      * Create a new array with length one. The array will be initialized with
0092      * the given value.
0093      *
0094      @param first the only element of the array.
0095      *
0096      @deprecated Use {@link #of(Object...)} instead.
0097      */
0098     @Deprecated
0099     public Array(final T first) {
0100         this(1);
0101         _array.data[0= first;
0102     }
0103 
0104     /**
0105      * Create a new array with length two. The array will be initialized with
0106      * the given values.
0107      *
0108      @param first first array element.
0109      @param second second array element.
0110      *
0111      @deprecated Use {@link #of(Object...)} instead.
0112      */
0113     @Deprecated
0114     public Array(
0115         final T first,
0116         final T second
0117     ) {
0118         this(2);
0119         _array.data[0= first;
0120         _array.data[1= second;
0121     }
0122 
0123     /**
0124      * Create a new array with length three. The array will be initialized with
0125      * the given values.
0126      *
0127      @param first first array element.
0128      @param second second array element.
0129      @param third third array element.
0130      *
0131      @deprecated Use {@link #of(Object...)} instead.
0132      */
0133     @Deprecated
0134     public Array(
0135         final T first,
0136         final T second,
0137         final T third
0138     ) {
0139         this(3);
0140         _array.data[0= first;
0141         _array.data[1= second;
0142         _array.data[2= third;
0143     }
0144 
0145     /**
0146      * Create a new array with length four. The array will be initialized with
0147      * the given values.
0148      *
0149      @param first first array element.
0150      @param second second array element.
0151      @param third third array element.
0152      @param fourth fourth array element.
0153      *
0154      @deprecated Use {@link #of(Object...)} instead.
0155      */
0156     @Deprecated
0157     public Array(
0158         final T first,
0159         final T second,
0160         final T third,
0161         final T fourth
0162     ) {
0163         this(4);
0164         _array.data[0= first;
0165         _array.data[1= second;
0166         _array.data[2= third;
0167         _array.data[3= fourth;
0168     }
0169 
0170     /**
0171      * Create a new array with length five. The array will be initialized with
0172      * the given values.
0173      *
0174      @param first first array element.
0175      @param second second array element.
0176      @param third third array element.
0177      @param fourth fourth array element.
0178      @param fifth fifth array element.
0179      *
0180      @deprecated Use {@link #of(Object...)} instead.
0181      */
0182     @Deprecated
0183     public Array(
0184         final T first,
0185         final T second,
0186         final T third,
0187         final T fourth,
0188         final T fifth
0189     ) {
0190         this(5);
0191         _array.data[0= first;
0192         _array.data[1= second;
0193         _array.data[2= third;
0194         _array.data[3= fourth;
0195         _array.data[4= fifth;
0196     }
0197 
0198     /**
0199      * Create a new array from the given values.
0200      *
0201      @param first first array element.
0202      @param second second array element.
0203      @param third third array element.
0204      @param fourth fourth array element.
0205      @param fifth fifth array element.
0206      @param rest the rest of the array element.
0207      @throws NullPointerException if the {@code rest} array is {@code null}.
0208      *
0209      @deprecated Use {@link #of(Object...)} instead.
0210      */
0211     @Deprecated
0212     @SafeVarargs
0213     public Array(
0214         final T first,
0215         final T second,
0216         final T third,
0217         final T fourth,
0218         final T fifth,
0219         final T... rest
0220     ) {
0221         this(+ rest.length);
0222         _array.data[0= first;
0223         _array.data[1= second;
0224         _array.data[2= third;
0225         _array.data[3= fourth;
0226         _array.data[4= fifth;
0227         arraycopy(rest, 0, _array.data, 5, rest.length);
0228     }
0229 
0230     /**
0231      * Create a new array from the given values.
0232      *
0233      @param values the array values.
0234      @throws NullPointerException if the {@code values} array is {@code null}.
0235      *
0236      @deprecated Use {@link #of(Object...)} instead.
0237      */
0238     @Deprecated
0239     public Array(final T[] values) {
0240         this(values.length);
0241         arraycopy(values, 0, _array.data, 0, values.length);
0242     }
0243 
0244     /**
0245      * Create a new Array from the values of the given Collection. The order of
0246      * the elements are determined by the iterator of the Collection.
0247      *
0248      @param values the array values.
0249      @throws NullPointerException if the {@code values} array is {@code null}.
0250      *
0251      @deprecated Use {@link #of(Collection)} instead.
0252      */
0253     @Deprecated
0254     public Array(final Collection<? extends T> values) {
0255         this(values.size());
0256 
0257         int index = 0;
0258         for (Iterator<? extends T>
0259             it = values.iterator(); it.hasNext(); ++index)
0260         {
0261             _array.data[index= it.next();
0262         }
0263     }
0264 
0265     /**
0266      * Selects all elements of this list which satisfy a predicate.
0267      *
0268      @param predicate the predicate used to test elements.
0269      @return a new array consisting of all elements of this list that satisfy
0270      *         the given {@code predicate}. The order of the elements is
0271      *         preserved.
0272      @throws NullPointerException if the given {@code predicate} is
0273      *         {@code null}.
0274      */
0275     public Array<T> filter(final Function<? super T, Boolean> predicate) {
0276         StackContext.enter();
0277         try {
0278             final FastList<T> filtered = FastList.newInstance();
0279             for (int i = 0, n = length(); i < n; ++i) {
0280                 @SuppressWarnings("unchecked")
0281                 final T value = (T)_array.data[i + _start];
0282 
0283                 if (predicate.apply(value== Boolean.TRUE) {
0284                     filtered.add(value);
0285                 }
0286             }
0287 
0288             final Array<T> copy = new Array<>(filtered.size());
0289             int index = 0;
0290             for (FastList.Node<T> n = filtered.head(), end = filtered.tail();
0291                 (n = n.getNext()) != end;)
0292             {
0293                 copy.set(index++, n.getValue());
0294             }
0295 
0296             return copy;
0297         finally {
0298             StackContext.exit();
0299         }
0300     }
0301 
0302     @Override
0303     public void set(final int index, final T value) {
0304         checkIndex(index);
0305 
0306         _array.cloneIfSealed();
0307         _array.data[index + _start= value;
0308     }
0309 
0310     /**
0311      <p>
0312      * Sorts the array of objects into ascending order, according to the natural
0313      * ordering of its elements. All elements in the array <b>must</b> implement
0314      * the Comparable interface. Furthermore, all elements in the array must be
0315      * mutually comparable.
0316      </p>
0317      * The sorting algorithm is the Quicksort.
0318      *
0319      @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0320      *          Wikipedia: Quicksort
0321      *      </a>
0322      *
0323      @throws ClassCastException if the array contains elements that are not
0324      *          <i>mutually comparable</i> (for example, strings and integers).
0325      */
0326     public void sort() {
0327         sort(0, length());
0328     }
0329 
0330     /**
0331      <p>
0332      * Sorts the array of objects into ascending order, according to the natural
0333      * ordering of its elements. All elements in the array <b>must</b> implement
0334      * the Comparable interface. Furthermore, all elements in the array must be
0335      * mutually comparable.
0336      </p>
0337      * The sorting algorithm is the Quicksort.
0338      *
0339      @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0340      *          Wikipedia: Quicksort
0341      *      </a>
0342      *
0343      @param from the index of the first element (inclusive) to be sorted.
0344      @param to the index of the last element (exclusive) to be sorted.
0345      @throws IndexOutOfBoundsException if {@code from < 0 or to > length()}
0346      @throws IllegalArgumentException if {@code from > to}
0347      @throws ClassCastException if the array contains elements that are not
0348      *        <i>mutually comparable</i> (for example, strings and integers).
0349      */
0350     public void sort(final int from, final int to) {
0351         sort(from, to, new Comparator<T>() {
0352             @SuppressWarnings({ "unchecked""rawtypes" })
0353             @Override
0354             public int compare(final T o1, final T o2) {
0355                 return ((Comparable)o1).compareTo(o2);
0356             }
0357         });
0358     }
0359 
0360     /**
0361      <p>
0362      * Sorts the array of objects according to the order induced by the specified
0363      * comparator. All elements in the array must be mutually comparable by the
0364      * specified comparator.
0365      </p>
0366      * The sorting algorithm is the Quicksort.
0367      *
0368      @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0369      *          Wikipedia: Quicksort
0370      *      </a>
0371      *
0372      @throws NullPointerException if the given {@code comparator} is
0373      *          {@code null}.
0374      @throws ClassCastException if the array contains elements that are not
0375      *          <i>mutually comparable</i> (for example, strings and integers).
0376      */
0377     public void sort(final Comparator<? super T> comparator) {
0378         sort(0, length(), comparator);
0379     }
0380 
0381     /**
0382      <p>
0383      * Sorts the array of objects according to the order induced by the specified
0384      * comparator. All elements in the array must be mutually comparable by the
0385      * specified comparator.
0386      </p>
0387      * The sorting algorithm is the <i>Timsort</i>.
0388      *
0389      @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Timsort">
0390      *          Wikipedia: Timsort
0391      *      </a>
0392      @see Arrays#sort(Object[], int, int, Comparator)
0393      *
0394      @param from the index of the first element (inclusive) to be sorted.
0395      @param to the index of the last element (exclusive) to be sorted.
0396      @throws NullPointerException if the given {@code comparator} is
0397      *          {@code null}.
0398      @throws IndexOutOfBoundsException if {@code from < 0 or to > length()}
0399      @throws IllegalArgumentException if {@code from > to}
0400      @throws ClassCastException if the array contains elements that are not
0401      *          <i>mutually comparable</i> (for example, strings and integers).
0402      */
0403     public void sort(
0404         final int from, final int to,
0405         final Comparator<? super T> comparator
0406     ) {
0407         checkIndex(from, to);
0408         if (from > to) {
0409             throw new IllegalArgumentException(format(
0410                     "From index > to index: %d > %d.", from, to
0411                 ));
0412         }
0413         requireNonNull(comparator, "Comparator");
0414 
0415         _array.cloneIfSealed();
0416 
0417         @SuppressWarnings("unchecked")
0418         final T[] data = (T[])_array.data;
0419         Arrays.sort(data, from + _start, to + _start, comparator);
0420     }
0421 
0422     private void uncheckedSwap(final int i, final int j) {
0423         final Object temp = _array.data[i + _start];
0424         _array.data[i + _start= _array.data[j + _start];
0425         _array.data[j + _start= temp;
0426     }
0427 
0428     /**
0429      * Reverses the given array in place.
0430      *
0431      @return this (reversed) array.
0432      */
0433     public Array<T> reverse() {
0434         return reverse(0, length());
0435     }
0436 
0437     /**
0438      * Reverses the part of the array determined by the to indexes. The reverse
0439      * method is performed in place.
0440      *
0441      @param from the first index (inclusive)
0442      @param to the second index (exclusive)
0443      @return this (reversed) array.
0444      @throws IllegalArgumentException if <tt>from &gt; to</tt>
0445      @throws IndexOutOfBoundsException if <tt>from &lt; 0</tt> or
0446      *            <tt>to &gt; a.length</tt>
0447      */
0448     public Array<T> reverse(final int from, final int to) {
0449         checkIndex(from, to);
0450         _array.cloneIfSealed();
0451 
0452         int i = from;
0453         int j = to;
0454         while (i < j) {
0455             uncheckedSwap(i++, --j);
0456         }
0457 
0458         return this;
0459     }
0460 
0461     @Override
0462     public void swap(final int i, final int j) {
0463         checkIndex(i);
0464         checkIndex(j);
0465 
0466         _array.cloneIfSealed();
0467         uncheckedSwap(i, j);
0468     }
0469 
0470     @Override
0471     public void swap(
0472         final int start, final int end,
0473         final MSeq<T> other, final int otherStart
0474     ) {
0475         if (other instanceof Array<?>) {
0476             swap(start, end, (Array<T>)other, otherStart);
0477         else {
0478             checkIndex(start, end);
0479             if (otherStart < || (otherStart + (end - start)) > _length) {
0480                 throw new ArrayIndexOutOfBoundsException(format(
0481                     "Invalid index range: [%d, %d)",
0482                     otherStart, (otherStart + (end - start))
0483                 ));
0484             }
0485 
0486             if (start < end) {
0487                 _array.cloneIfSealed();
0488 
0489                 for (int i = (end - start); --i >= 0;) {
0490                     @SuppressWarnings("unchecked")
0491                     final T temp = (T)_array.data[_start + start + i];
0492                     _array.data[_start + start + i= other.get(otherStart + i);
0493                     other.set(otherStart + i, temp);
0494                 }
0495             }
0496         }
0497     }
0498 
0499     /**
0500      @see MSeq#swap(int, int, MSeq, int)
0501      */
0502     public void swap(
0503         final int start, final int end,
0504         final Array<T> other, final int otherStart
0505     ) {
0506         checkIndex(start, end);
0507         other.checkIndex(otherStart, otherStart + (end - start));
0508 
0509         if (start < end) {
0510             _array.cloneIfSealed();
0511             other._array.cloneIfSealed();
0512 
0513             for (int i = (end - start); --i >= 0;) {
0514                 final Object temp = _array.data[_start + start + i];
0515                 _array.data[_start + start + i(
0516                     other._array.data[other._start + otherStart + i]
0517                 );
0518                 other._array.data[other._start + otherStart + i= temp;
0519             }
0520         }
0521     }
0522 
0523     /**
0524      * Randomize this array using the given {@link Random} object. The used
0525      * shuffling algorithm is from D. Knuth TAOCP, Seminumerical Algorithms,
0526      * Third edition, page 142, Algorithm S (Selection sampling technique).
0527      *
0528      @param random the {@link Random} object to use for randomize.
0529      @return this array
0530      @throws NullPointerException if the give random object is {@code null}.
0531      */
0532     public Array<T> shuffle(final Random random) {
0533         _array.cloneIfSealed();
0534 
0535         for (int j = length() 1; j > 0; --j) {
0536             uncheckedSwap(j, random.nextInt(j + 1));
0537         }
0538 
0539         return this;
0540     }
0541 
0542     /**
0543      * Randomize this array using the <i>registered</i> {@link Random} object.
0544      * The used shuffling algorithm is from D. Knuth TAOCP, Seminumerical
0545      * Algorithms, Third edition, page 142, Algorithm S (Selection sampling
0546      * technique).
0547      *
0548      @return this array
0549      */
0550     public Array<T> shuffle() {
0551         return shuffle(RandomRegistry.getRandom());
0552     }
0553 
0554     @Override
0555     public Array<T> setAll(final T value) {
0556         _array.cloneIfSealed();
0557         for (int i = _start; i < _end; ++i) {
0558             _array.data[i= value;
0559         }
0560         return this;
0561     }
0562 
0563     @Override
0564     public Array<T> setAll(final Iterator<? extends T> it) {
0565         _array.cloneIfSealed();
0566         for (int i = _start; i < _end && it.hasNext(); ++i) {
0567             _array.data[i= it.next();
0568         }
0569         return this;
0570     }
0571 
0572     @Override
0573     public Array<T> setAll(final Iterable<? extends T> values) {
0574         return setAll(values.iterator());
0575     }
0576 
0577     @Override
0578     public Array<T> setAll(final T[] values) {
0579         _array.cloneIfSealed();
0580         arraycopy(
0581             values, 0, _array.data, _start, min(length(), values.length)
0582         );
0583         return this;
0584     }
0585 
0586     @Override
0587     public Array<T> fill(final Factory<? extends T> factory) {
0588         requireNonNull(factory);
0589 
0590         _array.cloneIfSealed();
0591         for (int i = _start; i < _end; ++i) {
0592             _array.data[i= factory.newInstance();
0593         }
0594         return this;
0595     }
0596 
0597     @Override
0598     public ISeq<T> toISeq() {
0599         return new ArrayISeq<>(new ArrayRef(_array.seal().data), _start, _end);
0600     }
0601 
0602     /**
0603      * Create a new array which contains the values of {@code this} and the
0604      * given {@code value}. The length of the new array is
0605      * {@code this.length() + 1}. The returned array is not sealed.
0606      *
0607      @param value the value to append to this array.
0608      @return a new array which contains the values of {@code this} and the
0609      *          given {@code value}
0610      */
0611     public Array<T> add(final T value) {
0612         final Array<T> array = new Array<>(length() 1);
0613         arraycopy(_array.data, _start, array._array.data, 0, length());
0614         array._array.data[array.length() 1= value;
0615         return array;
0616     }
0617 
0618     /**
0619      * Create a new array which contains the values of {@code this} and the
0620      * given {@code array}. The length of the new array is
0621      * {@code this.length() + array.length()}. The returned array is not sealed.
0622      *
0623      @param array the array to append to this array.
0624      @return a new array which contains the values of {@code this} and the
0625      *          given {@code array}
0626      @throws NullPointerException if the {@code arrays} is {@code null}.
0627      */
0628     public Array<T> add(final Array<? extends T> array) {
0629         final Array<T> appended = new Array<>(length() + array.length());
0630 
0631         arraycopy(
0632             _array.data, _start,
0633             appended._array.data, 0, length()
0634         );
0635         arraycopy(
0636             array._array.data, array._start,
0637             appended._array.data, length(), array.length()
0638         );
0639 
0640         return appended;
0641     }
0642 
0643     /**
0644      * Create a new array which contains the values of {@code this} and the
0645      * given {@code values}. The length of the new array is
0646      * {@code this.length() + values.size()}. The returned array is not sealed.
0647      *
0648      @param values the array to append to this array.
0649      @return a new array which contains the values of {@code this} and the
0650      *          given {@code array}
0651      @throws NullPointerException if the {@code values} is {@code null}.
0652      */
0653     public Array<T> add(final Collection<? extends T> values) {
0654         requireNonNull(values, "Values");
0655         final Array<T> array = new Array<>(length() + values.size());
0656 
0657         arraycopy(_array.data, _start, array._array.data, 0, length());
0658         int index = length();
0659         for (Iterator<? extends T>
0660             it = values.iterator(); it.hasNext(); ++index)
0661         {
0662             array._array.data[index= it.next();
0663         }
0664 
0665         return array;
0666     }
0667 
0668     @Override
0669     public <B> Array<B> map(final Function<? super T, ? extends B> mapper) {
0670         requireNonNull(mapper, "Converter");
0671 
0672         final int length = length();
0673         final Array<B> result = new Array<>(length);
0674         assert (result._array.data.length == length);
0675 
0676         for (int i = length; --i >= 0;) {
0677             @SuppressWarnings("unchecked")
0678             final T value = (T)_array.data[i + _start];
0679             result._array.data[i= mapper.apply(value);
0680         }
0681         return result;
0682     }
0683 
0684     @Override
0685     public Array<T> copy() {
0686         return new Array<>(new ArrayRef(toArray())0, length());
0687     }
0688 
0689     @Override
0690     public Array<T> subSeq(final int start, final int end) {
0691         checkIndex(start, end);
0692         return new Array<>(_array, start + _start, _end + end - _length);
0693         //return new Array<>(_array, start + _start, end + _start);
0694     }
0695 
0696     @Override
0697     public Array<T> subSeq(final int start) {
0698         return subSeq(start, length());
0699     }
0700 
0701     @Override
0702     public List<T> asList() {
0703         return new ArrayMSeqList<>(this);
0704     }
0705 
0706     @Override
0707     public ListIterator<T> listIterator() {
0708         return new ArrayMSeqIterator<>(this);
0709     }
0710 
0711 
0712     /* *************************************************************************
0713      * Static factory methods.
0714      **************************************************************************/
0715 
0716     /**
0717      * Create a new array from the given values.
0718      *
0719      @param values the array values.
0720      @throws NullPointerException if the {@code values} array is {@code null}.
0721      */
0722     @SafeVarargs
0723     public static <T> Array<T> of(final T... values) {
0724         Array<T> array = empty();
0725         if (values.length > 0) {
0726             array = new Array<>(values.length);
0727             arraycopy(values, 0, array._array.data, 0, values.length);
0728         }
0729 
0730         return array;
0731     }
0732 
0733     /**
0734      @deprecated Use {@link #of(Object[])} instead.
0735      */
0736     @Deprecated
0737     @SafeVarargs
0738     public static <T> Array<T> valueOf(final T... values) {
0739         return of(values);
0740     }
0741 
0742     /**
0743      * Create a new Array from the values of the given {@code Collection}. The
0744      * order of the elements are determined by the iterator of the Collection.
0745      *
0746      @param values the array values.
0747      @throws NullPointerException if the {@code values} array is {@code null}.
0748      */
0749     public static <T> Array<T> of(final Collection<? extends T> values) {
0750         Array<T> array = empty();
0751         if (!values.isEmpty()) {
0752             array = new Array<>(values.size());
0753             int index = 0;
0754             for (Iterator<? extends T>
0755                 it = values.iterator(); it.hasNext(); ++index)
0756             {
0757                 array._array.data[index= it.next();
0758             }
0759         }
0760 
0761         return array;
0762     }
0763 
0764     /**
0765      @deprecated Use {@link #of(java.util.Collection)} instead.
0766      */
0767     @Deprecated
0768     public static <T> Array<T> valueOf(final Collection<? extends T> values) {
0769         return of(values);
0770     }
0771 
0772     /**
0773      * Create a new Array from the values of the given {@code Seq}.
0774      *
0775      @param values the array values.
0776      @throws NullPointerException if the {@code values} array is {@code null}.
0777      */
0778     public static <T> Array<T> of(final Seq<T> values) {
0779         Array<T> array = empty();
0780         if (values.length() 0) {
0781             if (values instanceof Array<?>) {
0782                 array = ((Array<T>)values).copy();
0783             else {
0784                 array = new Array<>(values.length());
0785                 int index = 0;
0786                 for (Iterator<? extends T>
0787                     it = values.iterator(); it.hasNext(); ++index)
0788                 {
0789                     array._array.data[index= it.next();
0790                 }
0791             }
0792         }
0793 
0794         return array;
0795     }
0796 
0797     /**
0798      @deprecated Use {@link #of(Seq)} instead.
0799      */
0800     @Deprecated
0801     public static <T> Array<T> valueOf(final Seq<T> values) {
0802         return of(values);
0803     }
0804 
0805     /**
0806      * Boxes the given native array into an {@code Array<Boolean>}.
0807      *
0808      @param values the native array to box.
0809      @return the boxed array.
0810      */
0811     public static Array<Boolean> box(final boolean... values) {
0812         Array<Boolean> array = empty();
0813         if (values.length > 0) {
0814             array = new Array<>(values.length);
0815             for (int i = values.length; --i >= 0;) {
0816                 array._array.data[i= values[i];
0817             }
0818         }
0819 
0820         return array;
0821     }
0822 
0823 
0824     /**
0825      * Boxes the given native array into an {@code Array<Char>}.
0826      *
0827      @param values the native array to box.
0828      @return the boxed array.
0829      */
0830     public static Array<Character> box(final char... values) {
0831         Array<Character> array = empty();
0832         if (values.length > 0) {
0833             array = new Array<>(values.length);
0834             for (int i = values.length; --i >= 0;) {
0835                 array._array.data[i= values[i];
0836             }
0837         }
0838 
0839         return array;
0840     }
0841 
0842     /**
0843      * Boxes the given native array into an {@code Array<Short>}.
0844      *
0845      @param values the native array to box.
0846      @return the boxed array.
0847      */
0848     public static Array<Short> box(final short... values) {
0849         Array<Short> array = empty();
0850         if (values.length > 0) {
0851             array = new Array<>(values.length);
0852             for (int i = values.length; --i >= 0;) {
0853                 array._array.data[i= values[i];
0854             }
0855         }
0856 
0857         return array;
0858     }
0859 
0860     /**
0861      * Boxes the given native array into an {@code Array<Integer>}.
0862      *
0863      @param values the native array to box.
0864      @return the boxed array.
0865      */
0866     public static Array<Integer> box(final int... values) {
0867         Array<Integer> array = empty();
0868         if (values.length > 0) {
0869             array = new Array<>(values.length);
0870             for (int i = values.length; --i >= 0;) {
0871                 array._array.data[i= values[i];
0872             }
0873         }
0874 
0875         return array;
0876     }
0877 
0878     /**
0879      * Boxes the given native array into an {@code Array<Long>}.
0880      *
0881      @param values the native array to box.
0882      @return the boxed array.
0883      */
0884     public static Array<Long> box(final long... values) {
0885         Array<Long> array = empty();
0886         if (values.length > 0) {
0887             array = new Array<>(values.length);
0888             for (int i = values.length; --i >= 0;) {
0889                 array._array.data[i= values[i];
0890             }
0891         }
0892 
0893         return array;
0894     }
0895 
0896     /**
0897      * Boxes the given native array into an {@code Array<Float>}.
0898      *
0899      @param values the native array to box.
0900      @return the boxed array.
0901      */
0902     public static Array<Float> box(final float... values) {
0903         Array<Float> array = empty();
0904         if (values.length > 0) {
0905             array = new Array<>(values.length);
0906             for (int i = values.length; --i >= 0;) {
0907                 array._array.data[i= values[i];
0908             }
0909         }
0910 
0911         return array;
0912     }
0913 
0914     /**
0915      * Boxes the given native array into an {@code Array<Double>}.
0916      *
0917      @param values the native array to box.
0918      @return the boxed array.
0919      */
0920     public static Array<Double> box(final double... values) {
0921         Array<Double> array = empty();
0922         if (values.length > 0) {
0923             array = new Array<>(values.length);
0924             for (int i = values.length; --i >= 0;) {
0925                 array._array.data[i= values[i];
0926             }
0927         }
0928 
0929         return array;
0930     }
0931 
0932     /**
0933      * Unboxes the given array to the corresponding native version.
0934      *
0935      @param values the {@code Array} to unbox.
0936      @return the unboxed native array.
0937      */
0938     public static boolean[] unboxBoolean(final Array<Boolean> values) {
0939         final boolean[] array = new boolean[values.length()];
0940         for (int i = values._start; i < values._end; ++i) {
0941             array[i - values._start(Boolean)values._array.data[i];
0942         }
0943 
0944         return array;
0945     }
0946 
0947     /**
0948      * Unboxes the given array to the corresponding native version.
0949      *
0950      @param values the {@code Array} to unbox.
0951      @return the unboxed native array.
0952      */
0953     public static char[] unboxChar(final Array<Character> values) {
0954         final char[] array = new char[values.length()];
0955         for (int i = values._start; i < values._end; ++i) {
0956             array[i - values._start(Character)values._array.data[i];
0957         }
0958 
0959         return array;
0960     }
0961 
0962     /**
0963      * Unboxes the given array to the corresponding native version.
0964      *
0965      @param values the {@code Array} to unbox.
0966      @return the unboxed native array.
0967      */
0968     public static short[] unboxShort(final Array<Short> values) {
0969         final short[] array = new short[values.length()];
0970         for (int i = values._start; i < values._end; ++i) {
0971             array[i - values._start(Short)values._array.data[i];
0972         }
0973 
0974         return array;
0975     }
0976 
0977     /**
0978      * Unboxes the given array to the corresponding native version.
0979      *
0980      @param values the {@code Array} to unbox.
0981      @return the unboxed native array.
0982      */
0983     public static int[] unboxInt(final Array<Integer> values) {
0984         final int[] array = new int[values.length()];
0985         for (int i = values._start; i < values._end; ++i) {
0986             array[i - values._start(Integer)values._array.data[i];
0987         }
0988 
0989         return array;
0990     }
0991 
0992     /**
0993      * Unboxes the given array to the corresponding native version.
0994      *
0995      @param values the {@code Array} to unbox.
0996      @return the unboxed native array.
0997      */
0998     public static long[] unboxLong(final Array<Long> values) {
0999         final long[] array = new long[values.length()];
1000         for (int i = values._start; i < values._end; ++i) {
1001             array[i - values._start(Long)values._array.data[i];
1002         }
1003 
1004         return array;
1005     }
1006 
1007     /**
1008      * Unboxes the given array to the corresponding native version.
1009      *
1010      @param values the {@code Array} to unbox.
1011      @return the unboxed native array.
1012      */
1013     public static float[] unboxFloat(final Array<Float> values) {
1014         final float[] array = new float[values.length()];
1015         for (int i = values._start; i < values._end; ++i) {
1016             array[i - values._start(Float)values._array.data[i];
1017         }
1018 
1019         return array;
1020     }
1021 
1022     /**
1023      * Unboxes the given array to the corresponding native version.
1024      *
1025      @param values the {@code Array} to unbox.
1026      @return the unboxed native array.
1027      */
1028     public static double[] unboxDouble(final Array<Double> values) {
1029         final double[] array = new double[values.length()];
1030         for (int i = values._start; i < values._end; ++i) {
1031             array[i - values._start(Double)values._array.data[i];
1032         }
1033 
1034         return array;
1035     }
1036 
1037 }