[android] use PropertyValue for default values in functions

This commit is contained in:
Ivo van Dongen
2017-03-05 01:06:44 -08:00
committed by Antonio Zugaldia
parent 0da6d8a646
commit 732cd8d20a
8 changed files with 169 additions and 76 deletions

View File

@@ -8,6 +8,7 @@ import com.mapbox.mapboxsdk.style.functions.stops.ExponentialStops;
import com.mapbox.mapboxsdk.style.functions.stops.IntervalStops;
import com.mapbox.mapboxsdk.style.functions.stops.Stop;
import com.mapbox.mapboxsdk.style.functions.stops.Stops;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import java.util.Map;
@@ -27,7 +28,7 @@ import java.util.Map;
public class CompositeFunction<Z extends Number, I, O> extends Function<Stop.CompositeValue<Z, I>, O> {
private final String property;
private O defaultValue;
private PropertyValue<O> defaultValue;
CompositeFunction(@NonNull String property,
@NonNull CategoricalStops<Stop.CompositeValue<Z, I>, O> stops) {
@@ -51,7 +52,7 @@ public class CompositeFunction<Z extends Number, I, O> extends Function<Stop.Com
private CompositeFunction(@Nullable O defaultValue, @NonNull String property,
@NonNull Stops<Stop.CompositeValue<Z, I>, O> stops) {
super(stops);
this.defaultValue = defaultValue;
this.defaultValue = new PropertyValue<>(property, defaultValue);
this.property = property;
}
@@ -61,7 +62,7 @@ public class CompositeFunction<Z extends Number, I, O> extends Function<Stop.Com
* @param defaultValue the default value to use when no other applies
* @return this (for chaining)
*/
public CompositeFunction<Z, I, O> withDefaultValue(O defaultValue) {
public CompositeFunction<Z, I, O> withDefaultValue(PropertyValue<O> defaultValue) {
this.defaultValue = defaultValue;
return this;
}
@@ -70,7 +71,7 @@ public class CompositeFunction<Z extends Number, I, O> extends Function<Stop.Com
* @return the defaultValue
*/
@Nullable
public O getDefaultValue() {
public PropertyValue<O> getDefaultValue() {
return defaultValue;
}
@@ -91,7 +92,7 @@ public class CompositeFunction<Z extends Number, I, O> extends Function<Stop.Com
Map<String, Object> valueObject = super.toValueObject();
valueObject.put(PROPERTY_KEY, property);
if (defaultValue != null) {
valueObject.put(DEFAULT_VALUE_KEY, defaultValue);
valueObject.put(DEFAULT_VALUE_KEY, defaultValue.value);
}
return valueObject;
}

View File

@@ -4,6 +4,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.style.functions.stops.Stops;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import java.util.Map;
@@ -25,7 +26,7 @@ import java.util.Map;
public class SourceFunction<I, O> extends Function<I, O> {
private final String property;
private O defaultValue;
private PropertyValue<O> defaultValue;
SourceFunction(@NonNull String property, @NonNull Stops<I, O> stops) {
this(null, property, stops);
@@ -37,7 +38,7 @@ public class SourceFunction<I, O> extends Function<I, O> {
private SourceFunction(@Nullable O defaultValue, @NonNull String property, @NonNull Stops<I, O> stops) {
super(stops);
this.property = property;
this.defaultValue = defaultValue;
this.defaultValue = defaultValue != null ? new PropertyValue<>(property, defaultValue) : null;
}
@@ -56,7 +57,7 @@ public class SourceFunction<I, O> extends Function<I, O> {
* @param defaultValue the default value to use when no other applies
* @return this (for chaining)
*/
public SourceFunction<I, O> withDefaultValue(O defaultValue) {
public SourceFunction<I, O> withDefaultValue(PropertyValue<O> defaultValue) {
this.defaultValue = defaultValue;
return this;
}
@@ -65,7 +66,7 @@ public class SourceFunction<I, O> extends Function<I, O> {
* @return the defaultValue
*/
@Nullable
public O getDefaultValue() {
public PropertyValue<O> getDefaultValue() {
return defaultValue;
}
@@ -77,7 +78,7 @@ public class SourceFunction<I, O> extends Function<I, O> {
Map<String, Object> valueObject = super.toValueObject();
valueObject.put(PROPERTY_KEY, property);
if (defaultValue != null) {
valueObject.put(DEFAULT_VALUE_KEY, defaultValue);
valueObject.put(DEFAULT_VALUE_KEY, defaultValue.value);
}
return valueObject;
}

View File

@@ -1,9 +1,12 @@
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.functions.Function;
import com.mapbox.mapboxsdk.utils.ColorUtils;
import timber.log.Timber;
@@ -15,7 +18,14 @@ public class PropertyValue<T> {
public final String name;
public final T value;
/* package */ PropertyValue(@NonNull String name, 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;
}
@@ -54,6 +64,22 @@ public class PropertyValue<T> {
}
}
@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;
}
}
@Override
public String toString() {
return String.format("%s: %s", name, value);