MappedAccumulator.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.util;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static org.jenetics.internal.util.object.eq;
025 
026 import org.jenetics.internal.util.HashBuilder;
027 
028 /**
029  * Abstract implementation of the {@link Accumulator} interface which defines a
030  * {@code samples} property which is incremented by the {@link #accumulate(Object)}
031  * method.
032  *
033  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
034  @since 1.0
035  @version 1.0 &mdash; <em>$Date: 2014-03-01 $</em>
036  */
037 public abstract class MappedAccumulator<T>
038     implements
039         Accumulator<T>,
040         Cloneable
041 {
042 
043     /**
044      * The number of accumulated samples.
045      */
046     protected long _samples = 0;
047 
048     protected MappedAccumulator() {
049     }
050 
051     /**
052      * Return the number of samples accumulated so far.
053      *
054      @return the number of samples accumulated so far.
055      */
056     public long getSamples() {
057         return _samples;
058     }
059 
060     @Override
061     public void accumulate(final T value) {
062         ++_samples;
063     }
064 
065     /**
066      * Return a view of this adapter with a different type {@code B}.
067      *
068      * Usage example:
069      * [code]
070      * // Convert a string on the fly into a double value.
071      * final Converter<String, Double> converter = new Converter<String, Double>() {
072      *         public Double convert(final String value) {
073      *             return Double.valueOf(value);
074      *         }
075      *     };
076      *
077      * // The values to accumulate
078      * final List<String> values = Arrays.asList("0", "1", "2", "3", "4", "5");
079      *
080      * final Accumulators.Min<Double> accumulator = new Accumulators.Min<Double>();
081      *
082      * // No pain to accumulate collections of a different type.
083      * Accumulators.accumulate(values, accumulator.map(converter));
084      * [/code]
085      *
086      @param <B> the type of the returned adapter (view).
087      @param mapper the mapper needed to map between the type of this
088      *        adapter and the adapter view type.
089      @return the adapter view with the different type.
090      @throws NullPointerException if the given {@code converter} is {@code null}.
091      */
092     public <B> MappedAccumulator<B> map(final Function<? super B, ? extends T> mapper) {
093         requireNonNull(mapper, "Mapper");
094         return new MappedAccumulator<B>() {
095             @Override
096             public void accumulate(final B value) {
097                 MappedAccumulator.this.accumulate(mapper.apply(value));
098             }
099         };
100     }
101 
102     @Override
103     public int hashCode() {
104         return HashBuilder.of(getClass()).and(_samples).value();
105     }
106 
107     @Override
108     public boolean equals(final Object obj) {
109         if (obj == this) {
110             return true;
111         }
112         if (obj == null || getClass() != obj.getClass()) {
113             return false;
114         }
115 
116         final MappedAccumulator<?> accumulator = (MappedAccumulator<?>)obj;
117         return eq(_samples, accumulator._samples);
118     }
119 
120     @Override
121     public String toString() {
122         return format(
123                 "%s[samples=%d]", getClass().getName(), _samples
124             );
125     }
126 
127     @SuppressWarnings("unchecked")
128     @Override
129     protected MappedAccumulator<T> clone() {
130         try {
131             return (MappedAccumulator<T>)super.clone();
132         catch (CloneNotSupportedException e) {
133             throw new AssertionError(e);
134         }
135     }
136 
137 }