001/**
002 * EnumerationPrimitive.java
003 *
004 * Copyright (c) 2004-2012, Nicole C. Tedesco.  All rights reserved.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at:
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package net.sf.jaccumulator.integers;
020
021import net.sf.jaccumulator.ConcurrencyStrategy;
022import net.sf.jaccumulator.StructureStrategy;
023import net.sf.jaccumulator.primitives.Primitive;
024import net.sf.jaccumulator.primitives.SealedPrimitive;
025import net.sf.jaccumulator.reals.SealedReal;
026import net.sf.jaccumulator.scalars.SealedScalar;
027
028/**
029 * A {@code int} precision {@link Primitive primitive} implementation
030 *
031 * @since JAccumulator 4.0
032 * @author Nicole Tedesco (<a
033 *         href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>)
034 */
035public final class EnumerationPrimitive
036    extends
037        AbstractModuloIntegerPrimitive<EnumerationPrimitive>
038{
039    private static final long serialVersionUID = -6318471188234713319L;
040
041    private Enum<?> _theValue;
042    private final Enum<?>[] _theValues;
043
044    protected EnumerationPrimitive( final Enum<?>[] values, final Enum<?> aValue )
045    {
046        super(values);
047        this._theValues = values;
048        this._theValue = aValue;
049    }
050
051    public EnumerationPrimitive()
052    {
053        this(Empty.values(), (Enum<?>)null);
054    }
055
056    public <E extends Enum<E>> EnumerationPrimitive(
057        final Class<E> anEnumerationClass )
058    {
059        this(anEnumerationClass.getEnumConstants());
060    }
061
062    public EnumerationPrimitive( final Enum<?> aValue )
063    {
064        this(aValue.getDeclaringClass().getEnumConstants(), aValue);
065    }
066
067    public EnumerationPrimitive( final Enum<?>[] values )
068    {
069        this(values, values.length == 0 ? null : values[0]);
070    }
071
072    private final Enum<?> get( final int aValue ) throws ArithmeticException {
073        return this._theValues[this.modulo(aValue)];
074    }
075
076    @Override
077    public final EnumerationPrimitive copy() {
078        return new EnumerationPrimitive(this._theValues, this._theValue);
079    }
080
081    @Override
082    public final EnumerationPrimitive copyUsing( final int aValue ) {
083        return new EnumerationPrimitive(this._theValues, this.get(aValue));
084    }
085
086    @Override
087    public final EnumerationPrimitive copyUsingText( final CharSequence aValue )
088    {
089        final String aDescription = aValue.toString();
090        for (int i = 0; i < this._theValues.length; i++) {
091            final Enum<?> e = this._theValues[i];
092            if (e.toString().equals(aDescription)) {
093                return new EnumerationPrimitive(this._theValues, e);
094            }
095        }
096        return this.copy();
097    }
098
099    @Override
100    public final ConcurrencyStrategy getConcurrency() {
101        return ConcurrencyStrategy.SEQUENTIAL;
102    }
103
104    @Override
105    public final StructureStrategy getStructureStrategy() {
106        return StructureStrategy.SINGLETON;
107    }
108
109    public final Enum<?> getValue() {
110        return this._theValue;
111    }
112
113    public final Enum<?>[] getValues() {
114        return this._theValues;
115    }
116
117    @Override
118    public final int intValue() {
119        return this._theValue.ordinal();
120    }
121
122    @Override
123    public <E extends Enum<E>> EnumerationPrimitive setEnumeration( final E aValue )
124        throws UnsupportedOperationException,
125            ArithmeticException,
126            IllegalArgumentException,
127            NullPointerException
128    {
129        return this.setScalar(aValue.ordinal());
130    }
131
132    @Override
133    public final EnumerationPrimitive setPrimitive( final SealedPrimitive<?> aValue )
134        throws UnsupportedOperationException,
135            IllegalArgumentException,
136            ArithmeticException,
137            NullPointerException
138    {
139        switch (aValue.getPrecision()) {
140        case NULL:
141        case BIT:
142        case BYTE:
143        case SHORT:
144        case CHARACTER:
145        case INTEGER:
146        case LONG:
147        case FLOAT:
148        case UNLIMITED_DECIMAL:
149        case UNLIMITED_INTEGER:
150            return this.setScalar(aValue.intValue());
151        }
152        return this.setText(aValue);
153    }
154
155    @Override
156    public final EnumerationPrimitive setReal( final SealedReal<?> aValue )
157        throws UnsupportedOperationException,
158            IllegalArgumentException,
159            ArithmeticException,
160            NullPointerException
161    {
162        return this.setScalar(aValue.intValue());
163    }
164
165    @Override
166    public final EnumerationPrimitive setScalar( final int aValue )
167        throws UnsupportedOperationException,
168            IllegalArgumentException,
169            ArithmeticException
170    {
171        this._theValue = this.get(aValue);
172        return this;
173    }
174
175    @Override
176    public final EnumerationPrimitive setScalar( final SealedScalar<?> aValue )
177        throws UnsupportedOperationException,
178            IllegalArgumentException,
179            ArithmeticException,
180            NullPointerException
181    {
182        return this.setScalar(aValue.intValue());
183    }
184
185    @Override
186    public EnumerationPrimitive setText( final CharSequence content )
187        throws UnsupportedOperationException,
188            NullPointerException,
189            IllegalArgumentException
190    {
191        final String aDescription = content.toString();
192        for (int i = 0; i < this._theValues.length; i++) {
193            final Enum<?> e = this._theValues[i];
194            if (e.toString().equals(aDescription)) {
195                this._theValue = e;
196                return this;
197            }
198        }
199        return this;
200    }
201
202    @Override
203    @SuppressWarnings( "unchecked" )
204    public final <E extends Enum<E>> E toEnumeration(
205        final Class<E> anEnumerationClass )
206        throws ArithmeticException,
207            IllegalArgumentException,
208            NullPointerException
209    {
210        if (anEnumerationClass == this._theValue.getDeclaringClass()) {
211            return (E)this._theValue;
212        }
213        return super.toEnumeration(anEnumerationClass);
214    }
215
216    @Override
217    public final String toString() {
218        return String.valueOf( this._theValue );
219    }
220
221    private static enum Empty
222    {
223    }
224}