001/**
002 * ModuloIntegerPrimitive.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"); you may not
007 * use this file except in compliance with the License. You may obtain a copy of
008 * 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, WITHOUT
014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015 * License for the specific language governing permissions and limitations under
016 * the License.
017 */
018
019package net.sf.jaccumulator.integers;
020
021import net.sf.jaccumulator.ConcurrencyStrategy;
022import net.sf.jaccumulator.Domain;
023import net.sf.jaccumulator.lex.To;
024import net.sf.jaccumulator.primitives.Primitive;
025
026/**
027 * A {@code int} precision {@link Primitive primitive} implementation
028 * 
029 * @since JAccumulator 4.0
030 * @author Nicole Tedesco (<a
031 *         href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>)
032 */
033public final class ModuloIntegerPrimitive
034    extends
035        AbstractModuloIntegerPrimitive<ModuloIntegerPrimitive>
036{
037    private static final long serialVersionUID = -3470125115763695203L;
038
039    private int _theValue;
040
041    public ModuloIntegerPrimitive()
042    {
043        super();
044        this._theValue = 0;
045    }
046
047    public ModuloIntegerPrimitive( final int aModularLimit )
048    {
049        super(aModularLimit);
050        this._theValue = 0;
051    }
052
053    public ModuloIntegerPrimitive( final Enum<?> anEnumeration )
054    {
055        super(anEnumeration);
056        this._theValue = anEnumeration.ordinal();
057    }
058
059    public ModuloIntegerPrimitive( final int aModularLimit, final int aValue )
060    {
061        this(aModularLimit);
062        this.setScalar(aValue);
063    }
064
065    @Override
066    public final ModuloIntegerPrimitive copy()
067    {
068        return new ModuloIntegerPrimitive(
069            this.getModularLimit(), this._theValue);
070    }
071
072    @Override
073    public final ModuloIntegerPrimitive copyUsing( final int aValue )
074    {
075        return new ModuloIntegerPrimitive(this.getModularLimit(), aValue);
076    }
077
078    @Override
079    public final ModuloIntegerPrimitive copyUsingText( final CharSequence aValue )
080    {
081        final Primitive<?> aModel = new IntegerPrimitive();
082        To.SCALAR.reactToText(aValue, aModel);
083        return new ModuloIntegerPrimitive(
084            this.getModularLimit(), aModel.intValue());
085    }
086
087    @Override
088    public final ConcurrencyStrategy getConcurrency()
089    {
090        return ConcurrencyStrategy.SEQUENTIAL;
091    }
092
093    @Override
094    public final Domain getDomain()
095    {
096        return Domain.MODULO;
097    }
098
099    @Override
100    public final int intValue()
101    {
102        return this._theValue;
103    }
104
105    @Override
106    public final ModuloIntegerPrimitive setScalar( final int aValue )
107        throws UnsupportedOperationException,
108            IllegalArgumentException,
109            ArithmeticException
110    {
111        this._theValue = this.modulo(aValue);
112        return this;
113    }
114
115    @Override
116    public final String toString()
117    {
118        return String.valueOf(this._theValue);
119    }
120}