142516d19db936b10874c27e16eeacda041af01f9George Mount/*
242516d19db936b10874c27e16eeacda041af01f9George Mount * Copyright (C) 2014 The Android Open Source Project
342516d19db936b10874c27e16eeacda041af01f9George Mount *
442516d19db936b10874c27e16eeacda041af01f9George Mount * Licensed under the Apache License, Version 2.0 (the "License");
542516d19db936b10874c27e16eeacda041af01f9George Mount * you may not use this file except in compliance with the License.
642516d19db936b10874c27e16eeacda041af01f9George Mount * You may obtain a copy of the License at
742516d19db936b10874c27e16eeacda041af01f9George Mount *
842516d19db936b10874c27e16eeacda041af01f9George Mount *      http://www.apache.org/licenses/LICENSE-2.0
942516d19db936b10874c27e16eeacda041af01f9George Mount *
1042516d19db936b10874c27e16eeacda041af01f9George Mount * Unless required by applicable law or agreed to in writing, software
1142516d19db936b10874c27e16eeacda041af01f9George Mount * distributed under the License is distributed on an "AS IS" BASIS,
1242516d19db936b10874c27e16eeacda041af01f9George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1342516d19db936b10874c27e16eeacda041af01f9George Mount * See the License for the specific language governing permissions and
1442516d19db936b10874c27e16eeacda041af01f9George Mount * limitations under the License.
1542516d19db936b10874c27e16eeacda041af01f9George Mount */
1642516d19db936b10874c27e16eeacda041af01f9George Mountpackage android.animation;
1742516d19db936b10874c27e16eeacda041af01f9George Mount
1842516d19db936b10874c27e16eeacda041af01f9George Mount/**
1942516d19db936b10874c27e16eeacda041af01f9George Mount * Abstract base class used convert type T to another type V and back again. This
2042516d19db936b10874c27e16eeacda041af01f9George Mount * is necessary when the value types of in animation are different from the property
2142516d19db936b10874c27e16eeacda041af01f9George Mount * type. BidirectionalTypeConverter is needed when only the final value for the
2242516d19db936b10874c27e16eeacda041af01f9George Mount * animation is supplied to animators.
2342516d19db936b10874c27e16eeacda041af01f9George Mount * @see PropertyValuesHolder#setConverter(TypeConverter)
2442516d19db936b10874c27e16eeacda041af01f9George Mount */
2542516d19db936b10874c27e16eeacda041af01f9George Mountpublic abstract class BidirectionalTypeConverter<T, V> extends TypeConverter<T, V> {
2642516d19db936b10874c27e16eeacda041af01f9George Mount    private BidirectionalTypeConverter mInvertedConverter;
2742516d19db936b10874c27e16eeacda041af01f9George Mount
2842516d19db936b10874c27e16eeacda041af01f9George Mount    public BidirectionalTypeConverter(Class<T> fromClass, Class<V> toClass) {
2942516d19db936b10874c27e16eeacda041af01f9George Mount        super(fromClass, toClass);
3042516d19db936b10874c27e16eeacda041af01f9George Mount    }
3142516d19db936b10874c27e16eeacda041af01f9George Mount
3242516d19db936b10874c27e16eeacda041af01f9George Mount    /**
3342516d19db936b10874c27e16eeacda041af01f9George Mount     * Does a conversion from the target type back to the source type. The subclass
3442516d19db936b10874c27e16eeacda041af01f9George Mount     * must implement this when a TypeConverter is used in animations and current
3542516d19db936b10874c27e16eeacda041af01f9George Mount     * values will need to be read for an animation.
3642516d19db936b10874c27e16eeacda041af01f9George Mount     * @param value The Object to convert.
3742516d19db936b10874c27e16eeacda041af01f9George Mount     * @return A value of type T, converted from <code>value</code>.
3842516d19db936b10874c27e16eeacda041af01f9George Mount     */
3942516d19db936b10874c27e16eeacda041af01f9George Mount    public abstract T convertBack(V value);
4042516d19db936b10874c27e16eeacda041af01f9George Mount
4142516d19db936b10874c27e16eeacda041af01f9George Mount    /**
4242516d19db936b10874c27e16eeacda041af01f9George Mount     * Returns the inverse of this converter, where the from and to classes are reversed.
4342516d19db936b10874c27e16eeacda041af01f9George Mount     * The inverted converter uses this convert to call {@link #convertBack(Object)} for
4442516d19db936b10874c27e16eeacda041af01f9George Mount     * {@link #convert(Object)} calls and {@link #convert(Object)} for
4542516d19db936b10874c27e16eeacda041af01f9George Mount     * {@link #convertBack(Object)} calls.
4642516d19db936b10874c27e16eeacda041af01f9George Mount     * @return The inverse of this converter, where the from and to classes are reversed.
4742516d19db936b10874c27e16eeacda041af01f9George Mount     */
4842516d19db936b10874c27e16eeacda041af01f9George Mount    public BidirectionalTypeConverter<V, T> invert() {
4942516d19db936b10874c27e16eeacda041af01f9George Mount        if (mInvertedConverter == null) {
5042516d19db936b10874c27e16eeacda041af01f9George Mount            mInvertedConverter = new InvertedConverter(this);
5142516d19db936b10874c27e16eeacda041af01f9George Mount        }
5242516d19db936b10874c27e16eeacda041af01f9George Mount        return mInvertedConverter;
5342516d19db936b10874c27e16eeacda041af01f9George Mount    }
5442516d19db936b10874c27e16eeacda041af01f9George Mount
5542516d19db936b10874c27e16eeacda041af01f9George Mount    private static class InvertedConverter<From, To> extends BidirectionalTypeConverter<From, To> {
5642516d19db936b10874c27e16eeacda041af01f9George Mount        private BidirectionalTypeConverter<To, From> mConverter;
5742516d19db936b10874c27e16eeacda041af01f9George Mount
5842516d19db936b10874c27e16eeacda041af01f9George Mount        public InvertedConverter(BidirectionalTypeConverter<To, From> converter) {
5942516d19db936b10874c27e16eeacda041af01f9George Mount            super(converter.getTargetType(), converter.getSourceType());
6042516d19db936b10874c27e16eeacda041af01f9George Mount            mConverter = converter;
6142516d19db936b10874c27e16eeacda041af01f9George Mount        }
6242516d19db936b10874c27e16eeacda041af01f9George Mount
6342516d19db936b10874c27e16eeacda041af01f9George Mount        @Override
6442516d19db936b10874c27e16eeacda041af01f9George Mount        public From convertBack(To value) {
6542516d19db936b10874c27e16eeacda041af01f9George Mount            return mConverter.convert(value);
6642516d19db936b10874c27e16eeacda041af01f9George Mount        }
6742516d19db936b10874c27e16eeacda041af01f9George Mount
6842516d19db936b10874c27e16eeacda041af01f9George Mount        @Override
6942516d19db936b10874c27e16eeacda041af01f9George Mount        public To convert(From value) {
7042516d19db936b10874c27e16eeacda041af01f9George Mount            return mConverter.convertBack(value);
7142516d19db936b10874c27e16eeacda041af01f9George Mount        }
7242516d19db936b10874c27e16eeacda041af01f9George Mount    }
7342516d19db936b10874c27e16eeacda041af01f9George Mount}
74