• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.animation;
17 
18 /**
19  * Abstract base class used convert type T to another type V and back again. This
20  * is necessary when the value types of in animation are different from the property
21  * type. BidirectionalTypeConverter is needed when only the final value for the
22  * animation is supplied to animators.
23  * @see PropertyValuesHolder#setConverter(TypeConverter)
24  */
25 public abstract class BidirectionalTypeConverter<T, V> extends TypeConverter<T, V> {
26     private BidirectionalTypeConverter mInvertedConverter;
27 
BidirectionalTypeConverter(Class<T> fromClass, Class<V> toClass)28     public BidirectionalTypeConverter(Class<T> fromClass, Class<V> toClass) {
29         super(fromClass, toClass);
30     }
31 
32     /**
33      * Does a conversion from the target type back to the source type. The subclass
34      * must implement this when a TypeConverter is used in animations and current
35      * values will need to be read for an animation.
36      * @param value The Object to convert.
37      * @return A value of type T, converted from <code>value</code>.
38      */
convertBack(V value)39     public abstract T convertBack(V value);
40 
41     /**
42      * Returns the inverse of this converter, where the from and to classes are reversed.
43      * The inverted converter uses this convert to call {@link #convertBack(Object)} for
44      * {@link #convert(Object)} calls and {@link #convert(Object)} for
45      * {@link #convertBack(Object)} calls.
46      * @return The inverse of this converter, where the from and to classes are reversed.
47      */
invert()48     public BidirectionalTypeConverter<V, T> invert() {
49         if (mInvertedConverter == null) {
50             mInvertedConverter = new InvertedConverter(this);
51         }
52         return mInvertedConverter;
53     }
54 
55     private static class InvertedConverter<From, To> extends BidirectionalTypeConverter<From, To> {
56         private BidirectionalTypeConverter<To, From> mConverter;
57 
InvertedConverter(BidirectionalTypeConverter<To, From> converter)58         public InvertedConverter(BidirectionalTypeConverter<To, From> converter) {
59             super(converter.getTargetType(), converter.getSourceType());
60             mConverter = converter;
61         }
62 
63         @Override
convertBack(To value)64         public From convertBack(To value) {
65             return mConverter.convert(value);
66         }
67 
68         @Override
convert(From value)69         public To convert(From value) {
70             return mConverter.convertBack(value);
71         }
72     }
73 }
74