/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.drawee.drawable;

import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;

/**
 * Set of properties for drawable. There are no default values and only gets applied if were set
 * explicitly.
 */
public class DrawableProperties {

  private final Property<Integer> mAlphaProperty = new Property<>();
  private final Property<ColorFilter> mColorFilterProperty = new Property<>();
  private final Property<Boolean> mDitherProperty = new Property<>();
  private final Property<Boolean> mFilterBitmapProperty = new Property<>();

  public void setAlpha(int alpha) {
    mAlphaProperty.setValue(alpha);
  }

  public void setColorFilter(ColorFilter colorFilter) {
    mColorFilterProperty.setValue(colorFilter);
  }

  public void setDither(boolean dither) {
    mDitherProperty.setValue(dither);
  }

  public void setFilterBitmap(boolean filterBitmap) {
    mFilterBitmapProperty.setValue(filterBitmap);
  }

  public void applyTo(Drawable drawable) {
    if (drawable == null) {
      return;
    }
    if (mAlphaProperty.mIsSet) {
      drawable.setAlpha(mAlphaProperty.mValue);
    }
    if (mColorFilterProperty.mIsSet) {
      drawable.setColorFilter(mColorFilterProperty.mValue);
    }
    if (mDitherProperty.mIsSet) {
      drawable.setDither(mDitherProperty.mValue);
    }
    if (mFilterBitmapProperty.mIsSet) {
      drawable.setFilterBitmap(mFilterBitmapProperty.mValue);
    }
  }

  private static class Property<T> {

    private T mValue;
    private boolean mIsSet;

    public void setValue(T value) {
      mValue = value;
      mIsSet = true;
    }
  }
}

@@ -591,92 +591,123 @@
 ate 
-final Property%3CInteger%3E mAlphaProperty = new Property%3C%3E();%0A  private final Property%3C
+static final int UNSET = -1;%0A%0A  private int mAlpha = UNSET;%0A  private boolean mIsSetColorFilter = false;%0A  private 
 Colo
@@ -713,17 +713,16 @@
 orFilter
-%3E
  mColorF
@@ -730,35 +730,15 @@
 lter
-Property = new Property%3C%3E()
+ = null
 ;%0A  
@@ -749,66 +749,27 @@
 ate 
-f
 in
-al Property%3CBoolean%3E mDitherProperty = new Property%3C%3E()
+t mDither = UNSET
 ;%0A  
@@ -780,72 +780,33 @@
 ate 
-f
 in
-al Property%3CBoolean%3E mFilterBitmapProperty = new Property%3C%3E()
+t mFilterBitmap = UNSET
 ;%0A%0A 
@@ -854,32 +854,16 @@
 lpha
-Property.setValue(
+ = 
 alpha
-)
 ;%0A  
@@ -941,27 +941,35 @@
 lter
-Property.setValue(c
+ = colorFilter;%0A    mIsSetC
 olor
@@ -974,17 +974,23 @@
 orFilter
-)
+ = true
 ;%0A  %7D%0A%0A 
@@ -1045,33 +1045,25 @@
 ther
-Property.setValue(dither)
+ = dither ? 1 : 0
 ;%0A  
@@ -1140,26 +1140,11 @@
 tmap
-Property.setValue(
+ = 
 filt
@@ -1151,17 +1151,24 @@
 erBitmap
-)
+ ? 1 : 0
 ;%0A  %7D%0A%0A 
@@ -1271,31 +1271,25 @@
  (mAlpha
-Property.mIsSet
+ != UNSET
 ) %7B%0A    
@@ -1314,31 +1314,16 @@
 a(mAlpha
-Property.mValue
 );%0A    %7D
@@ -1332,16 +1332,21 @@
    if (m
+IsSet
 ColorFil
@@ -1348,31 +1348,16 @@
 orFilter
-Property.mIsSet
 ) %7B%0A    
@@ -1394,31 +1394,16 @@
 orFilter
-Property.mValue
 );%0A    %7D
@@ -1418,31 +1418,25 @@
 (mDither
-Property.mIsSet
+ != UNSET
 ) %7B%0A    
@@ -1463,31 +1463,21 @@
 (mDither
-Property.mValue
+ != 0
 );%0A    %7D
@@ -1502,23 +1502,17 @@
 tmap
-Property.mIsSet
+ != UNSET
 ) %7B%0A
@@ -1559,203 +1559,14 @@
 tmap
-Property.mValue);%0A    %7D%0A  %7D%0A%0A  private static class Property%3CT%3E %7B%0A%0A    private T mValue;%0A    private boolean mIsSet;%0A%0A    public void setValue(T value) %7B%0A      mValue = value;%0A      mIsSet = true
+ != 0)
 ;%0A  

Improve allocations in DrawableProperties