ISeq.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.7.0).
003  * Copyright (c) 2007-2016 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.util;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.util.ArrayList;
025 import java.util.List;
026 import java.util.function.Function;
027 import java.util.function.Supplier;
028 import java.util.stream.Collector;
029 
030 import org.jenetics.internal.collection.Empty;
031 import org.jenetics.internal.util.require;
032 
033 /**
034  * Immutable, ordered, fixed sized sequence.
035  *
036  @see MSeq
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 3.6
041  */
042 public interface ISeq<T>
043     extends
044         Seq<T>,
045         Copyable<MSeq<T>>
046 {
047 
048     @Override
049     public ISeq<T> subSeq(final int start, final int end);
050 
051     @Override
052     public ISeq<T> subSeq(final int start);
053 
054     @Override
055     public <B> ISeq<B> map(final Function<? super T, ? extends B> mapper);
056 
057     @SuppressWarnings("unchecked")
058     @Override
059     public default ISeq<T> append(final T... values) {
060         return append(ISeq.of(values));
061     }
062 
063     @Override
064     public ISeq<T> append(final Iterable<? extends T> values);
065 
066     @SuppressWarnings("unchecked")
067     @Override
068     public default ISeq<T> prepend(final T... values) {
069         return prepend(ISeq.of(values));
070     }
071 
072     @Override
073     public ISeq<T> prepend(final Iterable<? extends T> values);
074 
075     /**
076      * Return a shallow copy of this sequence. The sequence elements are not
077      * cloned.
078      *
079      @return a shallow copy of this sequence.
080      */
081     @Override
082     public MSeq<T> copy();
083 
084 
085     /* *************************************************************************
086      *  Some static factory methods.
087      * ************************************************************************/
088 
089     /**
090      * Single instance of an empty {@code ISeq}.
091      *
092      @since 3.3
093      */
094     public static final ISeq<?> EMPTY = Empty.ISEQ;
095 
096     /**
097      * Return an empty {@code ISeq}.
098      *
099      @since 3.3
100      *
101      @param <T> the element type of the returned {@code ISeq}.
102      @return an empty {@code ISeq}.
103      */
104     public static <T> ISeq<T> empty() {
105         return Empty.iseq();
106     }
107 
108     /**
109      * Returns a {@code Collector} that accumulates the input elements into a
110      * new {@code ISeq}.
111      *
112      @param <T> the type of the input elements
113      @return a {@code Collector} which collects all the input elements into an
114      *         {@code ISeq}, in encounter order
115      */
116     public static <T> Collector<T, ?, ISeq<T>> toISeq() {
117         return Collector.of(
118             (Supplier<List<T>>)ArrayList::new,
119             List::add,
120             (left, right-> left.addAll(right)return left; },
121             ISeq::of
122         );
123     }
124 
125     /**
126      * Create a new {@code ISeq} from the given values.
127      *
128      @param <T> the element type
129      @param values the array values.
130      @return a new {@code ISeq} with the given values.
131      @throws NullPointerException if the {@code values} array is {@code null}.
132      */
133     @SafeVarargs
134     public static <T> ISeq<T> of(final T... values) {
135         return values.length == 0
136             ? empty()
137             : MSeq.of(values).toISeq();
138     }
139 
140     /**
141      * Create a new {@code ISeq} from the given values.
142      *
143      @param <T> the element type
144      @param values the array values.
145      @return a new {@code ISeq} with the given values.
146      @throws NullPointerException if the {@code values} array is {@code null}.
147      */
148     @SuppressWarnings("unchecked")
149     public static <T> ISeq<T> of(final Iterable<? extends T> values) {
150         requireNonNull(values);
151 
152         return values instanceof ISeq<?>
153             (ISeq<T>)values
154             : values instanceof MSeq<?>
155                 ((MSeq<T>)values).toISeq()
156                 : MSeq.<T>of(values).toISeq();
157     }
158 
159 //    /**
160 //     * Create a new {@code ISeq} instance from the remaining elements of the
161 //     * given iterator.
162 //     *
163 //     * @since 3.3
164 //     *
165 //     * @param <T> the element type.
166 //     * @return a new {@code ISeq} with the given remaining values.
167 //     * @throws NullPointerException if the {@code values} object is
168 //     *        {@code null}.
169 //     */
170 //    public static <T> ISeq<T> of(final Iterator<? extends T> values) {
171 //        final MSeq<T> seq = MSeq.of(values);
172 //        return seq.isEmpty() ? empty() : seq.toISeq();
173 //    }
174 
175     /**
176      * Creates a new sequence, which is filled with objects created be the given
177      * {@code supplier}.
178      *
179      @since 3.2
180      *
181      @param <T> the element type of the sequence
182      @param supplier the {@code Supplier} which creates the elements, the
183      *        returned sequence is filled with
184      @param length the length of the returned sequence
185      @return a new sequence filled with elements given by the {@code supplier}
186      @throws NegativeArraySizeException if the given {@code length} is
187      *         negative
188      @throws NullPointerException if the given {@code supplier} is
189      *         {@code null}
190      */
191     public static <T> ISeq<T> of(
192         final Supplier<? extends T> supplier,
193         final int length
194     ) {
195         requireNonNull(supplier);
196         require.nonNegative(length);
197 
198         return length == 0
199             ? empty()
200             : MSeq.<T>ofLength(length).fill(supplier).toISeq();
201     }
202 
203     /**
204      * Allows a safe (without compile warning) upcast from {@code B} to
205      * {@code A}. Since {@code ISeq} instances are immutable, an <i>upcast</i>
206      * will be always safe.
207      *
208      <pre>{@code
209      * // The sequence which we want to case.
210      * final ISeq<? extends Number> ints = ISeq.of(1, 2, 3, 4, 5);
211      *
212      * // This casts are possible without warning.
213      * final ISeq<Object> objects = ISeq.upcast(ints);
214      * final ISeq<Number> numbers = ISeq.upcast(ints);
215      *
216      * // This cast will, of course, still fail.
217      * final ISeq<String> strings = ISeq.upcast(ints);
218      * final ISeq<Integer> integers = ISeq.upcast(ints);
219      * }</pre>
220      *
221      @since 3.6
222      *
223      @param seq the sequence to cast safely
224      @param <A> the <i>super</i>-object type
225      @param <B> the <i>sub</i>-object type
226      @return the casted instance of the given {@code seq}
227      */
228     @SuppressWarnings("unchecked")
229     public static <A, B extends A> ISeq<A> upcast(final ISeq<B> seq) {
230         return (ISeq<A>)seq;
231     }
232 
233 }