001/**
002 * AbstractSimpleAccumulator.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;
020
021import net.sf.jaccumulator.primitives.Primitive;
022import net.sf.jaccumulator.primitives.Variant;
023import net.sf.jupperware.association.AssociativeList;
024
025/**
026 * Common behavior for a simple Accumulator with an independent, replaceable
027 * {@link #getTarget() target} Primitive
028 * 
029 * @param <ACCUMULATOR>
030 *        this {@link Accumulator} type
031 * 
032 * @since JAccumulator 4.0
033 * @author Nicole Tedesco (<a
034 *         href="mailto:nicole@tedesco.name">nicole@tedesco.name</a>)
035 */
036public abstract class AbstractSimpleAccumulator<ACCUMULATOR extends Accumulator<ACCUMULATOR>>
037    extends
038        AbstractAccumulator<ACCUMULATOR>
039{
040    private static final long serialVersionUID = -1477878184273286060L;
041
042    private Primitive<?> _theTarget;
043
044    public AbstractSimpleAccumulator()
045    {
046        this(new Variant());
047    }
048
049    public AbstractSimpleAccumulator( final Accumulator<?> anAccumulator )
050    {
051        super(anAccumulator);
052        this._theTarget = anAccumulator.getTarget();
053    }
054
055    public AbstractSimpleAccumulator( final Primitive<?> aTarget )
056    {
057        super();
058        this._theTarget = aTarget;
059    }
060
061    public AbstractSimpleAccumulator(
062        final Primitive<?> aTarget,
063        final AssociativeList<Accumulator<?>,Accumulator<?>> members )
064    {
065        super(members);
066        this._theTarget = aTarget;
067    }
068
069    @Override
070    public final Primitive<?> getTarget()
071    {
072        return this._theTarget;
073    }
074
075    @Override
076    public final boolean isTargetReplaceable()
077    {
078        return true;
079    }
080
081    @Override
082    @SuppressWarnings( "unchecked" )
083    public ACCUMULATOR shareTarget( final Primitive<?> aTarget )
084        throws UnsupportedOperationException,
085            NullPointerException,
086            IllegalStateException,
087            IllegalArgumentException
088    {
089        this._theTarget = aTarget;
090        return (ACCUMULATOR)this;
091    }
092}