summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyValue.java
blob: 44f58a54c258b21c8566e33dccc8f63b778bd8f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.mapbox.mapboxsdk.style.layers;

import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.mapboxsdk.exceptions.ConversionException;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.functions.Function;
import com.mapbox.mapboxsdk.utils.ColorUtils;

import timber.log.Timber;

/**
 * Properties for Layer
 */
public class PropertyValue<T> {

  public final String name;
  public final T value;

  /**
   * Not part of the public API.
   *
   * @param name  the property name
   * @param value the property value
   * @see PropertyFactory for construction of {@link PropertyValue}s
   */
  public PropertyValue(@NonNull String name, T value) {
    this.name = name;
    this.value = value;
  }

  /**
   * Returns if this is null
   *
   * @return true if this is null, false if not
   */
  public boolean isNull() {
    return value == null;
  }

  /**
   * Returns if this is a function.
   *
   * @return true if is a function, false if not
   */
  public boolean isFunction() {
    return !isNull() && value instanceof Function;
  }

  /**
   * Returns if this is a value.
   *
   * @return true if is a value, false if not
   */
  public boolean isValue() {
    return !isNull() && !isFunction();
  }

  /**
   * Returns if this is a expression.
   *
   * @return true if is a expression, false if not
   */
  public boolean isExpression() {
    return !isNull() && value instanceof Expression;
  }

  /**
   * Get the expression of the property.
   *
   * @return the expression value
   */
  @Nullable
  public Expression<T> getExpression() {
    if (isExpression()) {
      // noinspection unchecked
      return (Expression<T>) value;
    } else {
      Timber.e("not an expression, try value");
      return null;
    }
  }

  @Nullable
  public Function<?, T> getFunction() {
    if (isFunction()) {
      // noinspection unchecked
      return (Function<?, T>) value;
    } else {
      Timber.w("not a function, try value");
      return null;
    }
  }

  /**
   * Get the value of the property.
   *
   * @return the property value
   */
  @Nullable
  public T getValue() {
    if (isValue()) {
      // noinspection unchecked
      return (T) value;
    } else {
      Timber.w("not a value, try function");
      return null;
    }
  }

  /**
   * Get the color int value of the property if the value is a color.
   *
   * @return the color int value of the property, null if not a color value
   */
  @ColorInt
  @Nullable
  public Integer getColorInt() {
    if (!isValue() || !(value instanceof String)) {
      Timber.e("%s is not a String value and can not be converted to a color it", name);
      return null;
    }

    try {
      return ColorUtils.rgbaToColor((String) value);
    } catch (ConversionException ex) {
      Timber.e("%s could not be converted to a Color int: %s", name, ex.getMessage());
      return null;
    }
  }

  /**
   * Get the string representation of a property value.
   *
   * @return the string representation
   */
  @Override
  public String toString() {
    return String.format("%s: %s", name, value);
  }
}