Skip to content

Commit

Permalink
[android] use PropertyValue for default values in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen authored and zugaldia committed Mar 7, 2017
1 parent 0da6d8a commit 732cd8d
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;

Expand All @@ -27,7 +28,7 @@
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) {
Expand All @@ -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;
}

Expand All @@ -61,7 +62,7 @@ private CompositeFunction(@Nullable O defaultValue, @NonNull String property,
* @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;
}
Expand All @@ -70,7 +71,7 @@ public CompositeFunction<Z, I, O> withDefaultValue(O defaultValue) {
* @return the defaultValue
*/
@Nullable
public O getDefaultValue() {
public PropertyValue<O> getDefaultValue() {
return defaultValue;
}

Expand All @@ -91,7 +92,7 @@ public Map<String, Object> toValueObject() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;

import com.mapbox.mapboxsdk.style.functions.stops.Stops;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;

import java.util.Map;

Expand All @@ -25,7 +26,7 @@
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);
Expand All @@ -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;
}


Expand All @@ -56,7 +57,7 @@ public String getProperty() {
* @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;
}
Expand All @@ -65,7 +66,7 @@ public SourceFunction<I, O> withDefaultValue(O defaultValue) {
* @return the defaultValue
*/
@Nullable
public O getDefaultValue() {
public PropertyValue<O> getDefaultValue() {
return defaultValue;
}

Expand All @@ -77,7 +78,7 @@ public Map<String, Object> toValueObject() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -54,6 +64,22 @@ public T getValue() {
}
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testCircleRadiusAsCategoricalSourceFunction() {
categorical(
stop(1.0f, circleRadius(0.3f))
)
).withDefaultValue(0.3f)
).withDefaultValue(circleRadius(0.3f))
)
);

Expand All @@ -187,7 +187,9 @@ public void testCircleRadiusAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleRadius().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleRadius().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleRadius().getFunction().getStops().getClass());
assertEquals(0.3f, ((SourceFunction) layer.getCircleRadius().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleRadius().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleRadius().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getCircleRadius().getFunction()).getDefaultValue().getValue());
}

@Test
Expand All @@ -204,7 +206,7 @@ public void testCircleRadiusAsCompositeFunction() {
exponential(
stop(0, 0.3f, circleRadius(0.9f))
).withBase(0.5f)
).withDefaultValue(0.3f)
).withDefaultValue(circleRadius(0.3f))
)
);

Expand Down Expand Up @@ -320,7 +322,7 @@ public void testCircleColorAsCategoricalSourceFunction() {
categorical(
stop("valueA", circleColor(Color.RED))
)
)
).withDefaultValue(circleColor(Color.GREEN))
)
);

Expand All @@ -330,6 +332,9 @@ public void testCircleColorAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleColor().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleColor().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleColor().getFunction().getStops().getClass());
assertNotNull(((SourceFunction) layer.getCircleColor().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleColor().getFunction()).getDefaultValue().getValue());
assertEquals(Color.GREEN, (int) ((SourceFunction) layer.getCircleColor().getFunction()).getDefaultValue().getColorInt());
}

@Test
Expand Down Expand Up @@ -439,7 +444,7 @@ public void testCircleBlurAsCategoricalSourceFunction() {
categorical(
stop(1.0f, circleBlur(0.3f))
)
).withDefaultValue(0.3f)
).withDefaultValue(circleBlur(0.3f))
)
);

Expand All @@ -449,7 +454,9 @@ public void testCircleBlurAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleBlur().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleBlur().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleBlur().getFunction().getStops().getClass());
assertEquals(0.3f, ((SourceFunction) layer.getCircleBlur().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleBlur().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleBlur().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getCircleBlur().getFunction()).getDefaultValue().getValue());
}

@Test
Expand All @@ -466,7 +473,7 @@ public void testCircleBlurAsCompositeFunction() {
exponential(
stop(0, 0.3f, circleBlur(0.9f))
).withBase(0.5f)
).withDefaultValue(0.3f)
).withDefaultValue(circleBlur(0.3f))
)
);

Expand Down Expand Up @@ -582,7 +589,7 @@ public void testCircleOpacityAsCategoricalSourceFunction() {
categorical(
stop(1.0f, circleOpacity(0.3f))
)
).withDefaultValue(0.3f)
).withDefaultValue(circleOpacity(0.3f))
)
);

Expand All @@ -592,7 +599,9 @@ public void testCircleOpacityAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleOpacity().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleOpacity().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleOpacity().getFunction().getStops().getClass());
assertEquals(0.3f, ((SourceFunction) layer.getCircleOpacity().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleOpacity().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleOpacity().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getCircleOpacity().getFunction()).getDefaultValue().getValue());
}

@Test
Expand All @@ -609,7 +618,7 @@ public void testCircleOpacityAsCompositeFunction() {
exponential(
stop(0, 0.3f, circleOpacity(0.9f))
).withBase(0.5f)
).withDefaultValue(0.3f)
).withDefaultValue(circleOpacity(0.3f))
)
);

Expand Down Expand Up @@ -834,7 +843,7 @@ public void testCircleStrokeWidthAsCategoricalSourceFunction() {
categorical(
stop(1.0f, circleStrokeWidth(0.3f))
)
).withDefaultValue(0.3f)
).withDefaultValue(circleStrokeWidth(0.3f))
)
);

Expand All @@ -844,7 +853,9 @@ public void testCircleStrokeWidthAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleStrokeWidth().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleStrokeWidth().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleStrokeWidth().getFunction().getStops().getClass());
assertEquals(0.3f, ((SourceFunction) layer.getCircleStrokeWidth().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleStrokeWidth().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleStrokeWidth().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getCircleStrokeWidth().getFunction()).getDefaultValue().getValue());
}

@Test
Expand All @@ -861,7 +872,7 @@ public void testCircleStrokeWidthAsCompositeFunction() {
exponential(
stop(0, 0.3f, circleStrokeWidth(0.9f))
).withBase(0.5f)
).withDefaultValue(0.3f)
).withDefaultValue(circleStrokeWidth(0.3f))
)
);

Expand Down Expand Up @@ -977,7 +988,7 @@ public void testCircleStrokeColorAsCategoricalSourceFunction() {
categorical(
stop("valueA", circleStrokeColor(Color.RED))
)
)
).withDefaultValue(circleStrokeColor(Color.GREEN))
)
);

Expand All @@ -987,6 +998,9 @@ public void testCircleStrokeColorAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleStrokeColor().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleStrokeColor().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleStrokeColor().getFunction().getStops().getClass());
assertNotNull(((SourceFunction) layer.getCircleStrokeColor().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleStrokeColor().getFunction()).getDefaultValue().getValue());
assertEquals(Color.GREEN, (int) ((SourceFunction) layer.getCircleStrokeColor().getFunction()).getDefaultValue().getColorInt());
}

@Test
Expand Down Expand Up @@ -1096,7 +1110,7 @@ public void testCircleStrokeOpacityAsCategoricalSourceFunction() {
categorical(
stop(1.0f, circleStrokeOpacity(0.3f))
)
).withDefaultValue(0.3f)
).withDefaultValue(circleStrokeOpacity(0.3f))
)
);

Expand All @@ -1106,7 +1120,9 @@ public void testCircleStrokeOpacityAsCategoricalSourceFunction() {
assertEquals(SourceFunction.class, layer.getCircleStrokeOpacity().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getCircleStrokeOpacity().getFunction()).getProperty());
assertEquals(CategoricalStops.class, layer.getCircleStrokeOpacity().getFunction().getStops().getClass());
assertEquals(0.3f, ((SourceFunction) layer.getCircleStrokeOpacity().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleStrokeOpacity().getFunction()).getDefaultValue());
assertNotNull(((SourceFunction) layer.getCircleStrokeOpacity().getFunction()).getDefaultValue().getValue());
assertEquals(0.3f, ((SourceFunction) layer.getCircleStrokeOpacity().getFunction()).getDefaultValue().getValue());
}

@Test
Expand All @@ -1123,7 +1139,7 @@ public void testCircleStrokeOpacityAsCompositeFunction() {
exponential(
stop(0, 0.3f, circleStrokeOpacity(0.9f))
).withBase(0.5f)
).withDefaultValue(0.3f)
).withDefaultValue(circleStrokeOpacity(0.3f))
)
);

Expand Down
Loading

0 comments on commit 732cd8d

Please sign in to comment.