Params.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.tool.trial;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.io.Serializable;
025 import java.util.Iterator;
026 import java.util.List;
027 import java.util.stream.Stream;
028 
029 import javax.xml.bind.annotation.XmlAccessType;
030 import javax.xml.bind.annotation.XmlAccessorType;
031 import javax.xml.bind.annotation.XmlAttribute;
032 import javax.xml.bind.annotation.XmlElement;
033 import javax.xml.bind.annotation.XmlRootElement;
034 import javax.xml.bind.annotation.XmlType;
035 import javax.xml.bind.annotation.adapters.XmlAdapter;
036 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
037 
038 import org.jenetics.util.ISeq;
039 
040 /**
041  * Collection of parameters the function under test is tested with.
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044  @version 3.4
045  @since 3.4
046  */
047 @XmlJavaTypeAdapter(Params.Model.Adapter.class)
048 public final class Params<T> implements Iterable<T>, Serializable {
049 
050     private static final long serialVersionUID = 1L;
051 
052     private final String _name;
053     private final ISeq<T> _params;
054 
055     private Params(final String name, final ISeq<T> params) {
056         _name = requireNonNull(name);
057         _params = requireNonNull(params);
058     }
059 
060     /**
061      * Return the name of the parameter collection.
062      *
063      @return the name of the parameter collection
064      */
065     public String getName() {
066         return _name;
067     }
068 
069     /**
070      * Return the parameter with the given {@code index}.
071      *
072      @param index the parameter index
073      @return the parameter with the given {@code index}
074      @throws IndexOutOfBoundsException if the {@code index} is out of range
075      *         {@code (index < 0 || index >= size())}
076      */
077     public T get(final int index) {
078         return _params.get(index);
079     }
080 
081     /**
082      * Return the number of parameters this collection contains.
083      *
084      @return the number of parameters
085      */
086     public int size() {
087         return _params.size();
088     }
089 
090     /**
091      * Return the parameter values.
092      *
093      @return the parameter values
094      */
095     public ISeq<T> values() {
096         return _params;
097     }
098 
099     /**
100      * Return the parameter values as stream.
101      *
102      @return the parameter values as stream
103      */
104     public Stream<T> stream() {
105         return _params.stream();
106     }
107 
108     @Override
109     public Iterator<T> iterator() {
110         return _params.iterator();
111     }
112 
113     @Override
114     public int hashCode() {
115         return _params.hashCode();
116     }
117 
118     @Override
119     public boolean equals(final Object obj) {
120         return obj instanceof Params<?> &&
121             _params.equals(((Params<?>)obj)._params);
122     }
123 
124     @Override
125     public String toString() {
126         return _params.toString();
127     }
128 
129     /**
130      * Return a new parameters object.
131      *
132      @param name the name of the parameters
133      @param params the actual parameters
134      @param <T> the parameter type
135      @throws NullPointerException if one of the parameters is {@code null}
136      @return a new parameters object
137      */
138     public static <T> Params<T> of(
139         final String name,
140         final ISeq<T> params
141     ) {
142         return new Params<>(name, params);
143     }
144 
145     /* *************************************************************************
146      *  JAXB object serialization
147      * ************************************************************************/
148 
149     @XmlRootElement(name = "params")
150     @XmlType(name = "org.jenetics.tool.Params")
151     @XmlAccessorType(XmlAccessType.FIELD)
152     @SuppressWarnings({"unchecked""rawtypes"})
153     static final class Model {
154 
155         @XmlAttribute(name = "name")
156         public String name;
157 
158         @XmlElement(name = "param", required = true, nillable = false)
159         public List params;
160 
161         public static final class Adapter extends XmlAdapter<Model, Params> {
162             @Override
163             public Model marshal(final Params params) {
164                 final Model model = new Model();
165                 model.name = params.getName();
166                 model.params = params.values().asList();
167                 return model;
168             }
169 
170             @Override
171             public Params unmarshal(final Model model) {
172                 return Params.of(
173                     model.name,
174                     ISeq.of(model.params)
175                 );
176             }
177         }
178     }
179 
180 }