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