CompositeAlterer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.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;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static org.jenetics.internal.util.object.NonNull;
025 import static org.jenetics.internal.util.object.eq;
026 
027 import java.util.Deque;
028 import java.util.LinkedList;
029 import java.util.List;
030 import java.util.concurrent.atomic.AtomicInteger;
031 
032 import org.jenetics.internal.util.HashBuilder;
033 
034 import org.jenetics.util.Array;
035 import org.jenetics.util.Function;
036 import org.jenetics.util.ISeq;
037 import org.jenetics.util.Seq;
038 
039 /**
040  * Combines several alterers to one.
041  *
042  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
043  @since 1.0
044  @version 1.2 &mdash; <em>$Date: 2014-03-01 $</em>
045  */
046 public final class CompositeAlterer<G extends Gene<?, G>>
047     extends AbstractAlterer<G>
048 {
049 
050     private final ISeq<Alterer<G>> _alterers;
051 
052     /**
053      * Combine the given alterers.
054      *
055      @param alterers the alterers to combine.
056      @throws NullPointerException if one of the alterers is {@code null}.
057      */
058     public CompositeAlterer(final Seq<Alterer<G>> alterers) {
059         super(1.0);
060 
061         alterers.forAll(NonNull("Alterer"));
062         _alterers = normalize(alterers).toISeq();
063     }
064 
065     /**
066      * Combine the given alterers.
067      *
068      @param alterers the alterers to combine.
069      @throws NullPointerException if one of the alterers is {@code null}.
070      *
071      @deprecated Use {@link #of(Alterer...)} instead.
072      */
073     @Deprecated
074     @SafeVarargs
075     public CompositeAlterer(final Alterer<G>... alterers) {
076         this(Array.of(alterers));
077     }
078 
079     private static <G extends Gene<?, G>>
080     Array<Alterer<G>> normalize(final Seq<Alterer<G>> alterers) {
081         final Deque<Alterer<G>> stack = new LinkedList<>(alterers.asList());
082 
083         final List<Alterer<G>> normalized = new LinkedList<>();
084 
085         while (!stack.isEmpty()) {
086             final Alterer<G> alterer = stack.pollFirst();
087 
088             if (alterer instanceof CompositeAlterer<?>) {
089                 final CompositeAlterer<G> calterer = (CompositeAlterer<G>)alterer;
090 
091                 for (int i = calterer.getAlterers().length(); --i >= 0;) {
092                     stack.addFirst(calterer.getAlterers().get(i));
093                 }
094             else {
095                 normalized.add(alterer);
096             }
097         }
098 
099         return Array.of(normalized);
100     }
101 
102     @Override
103     public <C extends Comparable<? super C>>
104     int alter(final Population<G, C> population, final int generation) {
105         final AtomicInteger alterations = new AtomicInteger(0);
106 
107         _alterers.forEach(new Function<Alterer<G>, Void>() {
108             @Override public Void apply(final Alterer<G> alterer) {
109                 alterations.addAndGet(alterer.alter(population, generation));
110                 return null;
111             }
112         });
113 
114         return alterations.get();
115     }
116 
117     /**
118      * Create a new CompositeAlterer with the given alterer appended.
119      *
120      @param alterer the alterer to append.
121      @return a new CompositeAlterer.
122      @throws NullPointerException if the given alterer is {@code null}.
123      */
124     public CompositeAlterer<G> append(final Alterer<G> alterer) {
125         return CompositeAlterer.valueOf(this, requireNonNull(alterer, "Alterer"));
126     }
127 
128     /**
129      * Return the alterers this alterer consists of. The returned array is sealed
130      * and cannot be changed.
131      *
132      @return the alterers this alterer consists of.
133      */
134     public ISeq<Alterer<G>> getAlterers() {
135         return _alterers;
136     }
137 
138     @Override
139     public int hashCode() {
140         return HashBuilder.of(getClass()).and(_alterers).value();
141     }
142 
143     @Override
144     public boolean equals(final Object obj) {
145         if (obj == this) {
146             return true;
147         }
148         if (obj == null || obj.getClass() != getClass()) {
149             return false;
150         }
151 
152         final CompositeAlterer<?> alterer = (CompositeAlterer<?>)obj;
153         return eq(_alterers, alterer._alterers);
154     }
155 
156     @Override
157     public String toString() {
158         return format("%s[%s]", getClass().getSimpleName(), _alterers);
159     }
160 
161     /**
162      * Combine the given alterers.
163      *
164      @param alterers the alterers to combine.
165      @throws NullPointerException if one of the alterers is {@code null}.
166      *
167      @deprecated Use {@link #of(Alterer[])} instead.
168      */
169     @Deprecated
170     @SafeVarargs
171     public static <G extends Gene<?, G>>
172     CompositeAlterer<G> valueOf(final Alterer<G>... alterers) {
173         return new CompositeAlterer<>(Array.of(alterers));
174     }
175 
176     /**
177      * Combine the given alterers.
178      *
179      @param alterers the alterers to combine.
180      @throws NullPointerException if one of the alterers is {@code null}.
181      */
182     @SafeVarargs
183     public static <G extends Gene<?, G>>
184     CompositeAlterer<G> of(final Alterer<G>... alterers) {
185         return new CompositeAlterer<>(Array.of(alterers));
186     }
187 
188     /**
189      * Joins the given alterer and returns a new CompositeAlterer object. If one
190      * of the given alterers is a CompositeAlterer the sub alterers of it are
191      * unpacked and appended to the newly created CompositeAlterer.
192      *
193      @param <T> the gene type of the alterers.
194      @param a1 the first alterer.
195      @param a2 the second alterer.
196      @return a new CompositeAlterer object.
197      @throws NullPointerException if one of the given alterer is {@code null}.
198      */
199     public static <T extends Gene<?, T>> CompositeAlterer<T> join(
200         final Alterer<T> a1,
201         final Alterer<T> a2
202     ) {
203         return CompositeAlterer.valueOf(a1, a2);
204     }
205 }