ISeq.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.0.0).
003  * Copyright (c) 2007-2014 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 java.util.ArrayList;
023 import java.util.List;
024 import java.util.function.Function;
025 import java.util.function.Supplier;
026 import java.util.stream.Collector;
027 
028 /**
029  * Immutable, ordered, fixed sized sequence.
030  *
031  @see MSeq
032  *
033  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
034  @since 1.0
035  @version 3.0 &mdash; <em>$Date: 2014-12-28 $</em>
036  */
037 public interface ISeq<T>
038     extends
039         Seq<T>,
040         Copyable<MSeq<T>>
041 {
042 
043     @Override
044     public ISeq<T> subSeq(final int start, final int end);
045 
046     @Override
047     public ISeq<T> subSeq(final int start);
048 
049     @Override
050     public <B> ISeq<B> map(final Function<? super T, ? extends B> mapper);
051 
052     /**
053      * Return a shallow copy of this sequence. The sequence elements are not
054      * cloned.
055      *
056      @return a shallow copy of this sequence.
057      */
058     @Override
059     public MSeq<T> copy();
060 
061 
062     /* *************************************************************************
063      *  Some static factory methods.
064      * ************************************************************************/
065 
066     /**
067      * Returns a {@code Collector} that accumulates the input elements into a
068      * new {@code ISeq}.
069      *
070      @param <T> the type of the input elements
071      @return a {@code Collector} which collects all the input elements into an
072      *         {@code ISeq}, in encounter order
073      */
074     public static <T> Collector<T, ?, ISeq<T>> toISeq() {
075         return Collector.of(
076             (Supplier<List<T>>)ArrayList::new,
077             List::add,
078             (left, right-> left.addAll(right)return left; },
079             ISeq::of
080         );
081     }
082 
083     /**
084      * Create a new {@code ISeq} from the given values.
085      *
086      @param <T> the element type
087      @param values the array values.
088      @return a new {@code ISeq} with the given values.
089      @throws NullPointerException if the {@code values} array is {@code null}.
090      */
091     @SafeVarargs
092     public static <T> ISeq<T> of(final T... values) {
093         return MSeq.of(values).toISeq();
094     }
095 
096     /**
097      * Create a new {@code ISeq} from the given values.
098      *
099      @param <T> the element type
100      @param values the array values.
101      @return a new {@code ISeq} with the given values.
102      @throws NullPointerException if the {@code values} array is {@code null}.
103      */
104     @SuppressWarnings("unchecked")
105     public static <T> ISeq<T> of(final Iterable<? extends T> values) {
106         return values instanceof ISeq<?> ?
107             (ISeq<T>)values :
108             values instanceof MSeq<?> ?
109                 ((MSeq<T>)values).toISeq() :
110                 MSeq.of(values).toISeq();
111     }
112 
113     public static <T> ISeq<T> of(Supplier<? extends T> supplier, final int length) {
114         return MSeq.<T>ofLength(length).fill(supplier).toISeq();
115     }
116 
117 }