001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/content/animation/src/main/jtemp/org/openimaj/content/animation/animator/#TT#ArrayValueAnimator.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.content.animation.animator;
035
036/**
037 * A {@link ValueAnimator} capable of producing byte arrays from
038 * a set of potentially independent underlying {@link ValueAnimator}s. 
039 * 
040 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
041 */
042public class ByteArrayValueAnimator implements ValueAnimator<byte[]> {
043        ValueAnimator<Byte> [] animators;
044        
045        /**
046         * Construct from an array of {@link ValueAnimator}s
047         * @param animators the animators
048         */
049        @SafeVarargs
050        public ByteArrayValueAnimator(ValueAnimator<Byte>... animators) {
051                this.animators = animators;
052        }
053        
054        /**
055         * Construct a new ByteArrayValueAnimator from an array of 
056         * values using {@link LinearByteValueAnimator}s with the 
057         * range -max to +max and the given duration.
058         * 
059         * @param duration the duration of the underlying animators 
060         * @param maxs max distance from 0 (+/-)
061         * @return new {@link ByteArrayValueAnimator}
062         */
063        @SuppressWarnings("unchecked")
064        public static ByteArrayValueAnimator makeLinear(int duration, byte... maxs) {
065                ValueAnimator<Byte> [] animators = new ValueAnimator[maxs.length];
066                
067                for (int i=0; i<maxs.length; i++)
068                        animators[i] = new LinearByteValueAnimator((byte)(-maxs[i]), maxs[i], duration);
069                
070                return new ByteArrayValueAnimator(animators);
071        }
072        
073        /**
074         * Construct a new ByteArrayValueAnimator from count 
075         * {@link LinearByteValueAnimator}s with the given start 
076         * and finish values, and the given duration.
077         * 
078         * @param duration the duration
079         * @param count the number of animators
080         * @param start the starting value
081         * @param finish the finishing value
082         * @return new {@link ByteArrayValueAnimator}
083         */
084        @SuppressWarnings("unchecked")
085        public static ByteArrayValueAnimator makeLinear(int duration, int count, byte start, byte finish) {
086                ValueAnimator<Byte> [] animators = new ValueAnimator[count];
087                
088                for (int i=0; i<count; i++)
089                        animators[i] = new LinearByteValueAnimator(start, finish, duration);
090                
091                return new ByteArrayValueAnimator(animators);
092        }
093        
094        /**
095         * Construct a new ByteArrayValueAnimator from an array of 
096         * values using {@link RandomLinearByteValueAnimator}s with the 
097         * range -max to +max and the given duration.
098         * 
099         * @param duration the duration of the underlying animators 
100         * @param maxs max distance from 0 (+/-)
101         * @return new {@link ByteArrayValueAnimator}
102         */
103        @SuppressWarnings("unchecked")
104        public static ByteArrayValueAnimator makeRandomLinear(int duration, byte... maxs) {
105                ValueAnimator<Byte> [] animators = new ValueAnimator[maxs.length];
106                
107                for (int i=0; i<maxs.length; i++)
108                        animators[i] = new RandomLinearByteValueAnimator((byte)(-maxs[i]), maxs[i], duration);
109                
110                return new ByteArrayValueAnimator(animators);
111        }
112        
113        /**
114         * Construct a new ByteArrayValueAnimator from count 
115         * {@link RandomLinearByteValueAnimator}s with the given start 
116         * and finish values, and the given duration.
117         * 
118         * @param duration the duration
119         * @param count the number of animators
120         * @param start the starting value
121         * @param finish the finishing value
122         * @return new {@link ByteArrayValueAnimator}
123         */
124        @SuppressWarnings("unchecked")
125        public static ByteArrayValueAnimator makeRandomLinear(int duration, int count, byte start, byte finish) {
126                ValueAnimator<Byte> [] animators = new ValueAnimator[count];
127                
128                for (int i=0; i<count; i++)
129                        animators[i] = new RandomLinearByteValueAnimator(start, finish, duration);
130                
131                return new ByteArrayValueAnimator(animators);
132        }
133        
134        @Override
135        public byte[] nextValue() {
136                byte[] value = new byte[animators.length];
137                
138                for (int i=0; i<animators.length; i++)
139                        value[i] = animators[i].nextValue();
140                
141                return value;
142        }
143
144        @Override
145        public boolean hasFinished() {
146                for (ValueAnimator<Byte> a : animators) {
147                        if (!a.hasFinished())
148                                return false;
149                }
150                return true;
151        }
152
153        @Override
154        public void reset() {
155                for (ValueAnimator<Byte> animator : animators)
156                        animator.reset();
157        }
158}