summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/CategoricalStops.java64
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/ExponentialStops.java97
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IdentityStops.java18
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IntervalStops.java58
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IterableStops.java52
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stop.java109
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stops.java99
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/package-info.java6
8 files changed, 503 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/CategoricalStops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/CategoricalStops.java
new file mode 100644
index 0000000000..f9b2929350
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/CategoricalStops.java
@@ -0,0 +1,64 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import android.support.annotation.NonNull;
+import android.support.annotation.Size;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * {@link Stops} implementation for categorical functions
+ *
+ * @param <I> the {@link Stop} input type
+ * @param <O> the {@link Stop} output type
+ */
+public class CategoricalStops<I, O> extends IterableStops<I, O, Stop<I, O>> {
+
+ private final Stop<I, O>[] stops;
+
+ /**
+ * Create a categorical {@link Stops} implementation. Use through {@link Stops#categorical(Stop[])}
+ *
+ * @param stops the stops
+ */
+ @SafeVarargs
+ public CategoricalStops(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ this.stops = stops;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Map<String, Object> toValueObject() {
+ Map<String, Object> map = super.toValueObject();
+ map.put("stops", toValueObjects(stops));
+ return map;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getTypeName() {
+ return "categorical";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Iterator<Stop<I, O>> iterator() {
+ return Arrays.asList(stops).iterator();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int size() {
+ return stops.length;
+ }
+
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/ExponentialStops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/ExponentialStops.java
new file mode 100644
index 0000000000..d47aa1fc91
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/ExponentialStops.java
@@ -0,0 +1,97 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import android.support.annotation.NonNull;
+import android.support.annotation.Size;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * The {@link Stops} implementation for exponential functions
+ *
+ * @param <I> the input type
+ * @param <O> the output type
+ */
+public class ExponentialStops<I, O> extends IterableStops<I, O, Stop<I, O>> {
+
+ private float base;
+ private final Stop<I, O>[] stops;
+
+ /**
+ * Create exponential stops with an explicit base. Use through {@link Stops#exponential(Stop[])}
+ *
+ * @param base The exponential base of the interpolation curve. It controls the rate at which the function output
+ * increases. Higher values make the output increase more towards the high end of the range.
+ * With values close to 1 the output increases linearly.
+ * @param stops the stops
+ */
+ @SafeVarargs
+ public ExponentialStops(Float base, @NonNull @Size(min = 1) Stop<I, O>... stops) {
+ this.base = base != null ? base : 1.0f;
+ this.stops = stops;
+ }
+
+ /**
+ * Create exponential stops without an explicit base. Use through {@link Stops#exponential(Stop[])}
+ *
+ * @param stops the stops
+ */
+ @SafeVarargs
+ public ExponentialStops(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ this(null, stops);
+ }
+
+ /**
+ * Set the exponential base
+ *
+ * @param base the base to use in the exponential function
+ * @return this (for chaining)
+ */
+ public ExponentialStops<I, O> withBase(float base) {
+ this.base = base;
+ return this;
+ }
+
+ /**
+ * @return The exponential base
+ */
+ public float getBase() {
+ return base;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Map<String, Object> toValueObject() {
+ Map<String, Object> map = super.toValueObject();
+ map.put("base", base);
+ map.put("stops", toValueObjects(stops));
+ return map;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getTypeName() {
+ return "exponential";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Iterator<Stop<I, O>> iterator() {
+ return Arrays.asList(stops).iterator();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int size() {
+ return stops.length;
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IdentityStops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IdentityStops.java
new file mode 100644
index 0000000000..2c0b198dc2
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IdentityStops.java
@@ -0,0 +1,18 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+/**
+ * The {@link Stops} implementation for identity functions
+ *
+ * @param <T> the input/output type
+ */
+public class IdentityStops<T> extends Stops<T, T> {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected String getTypeName() {
+ return "identity";
+ }
+
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IntervalStops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IntervalStops.java
new file mode 100644
index 0000000000..9d95b3f8c1
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IntervalStops.java
@@ -0,0 +1,58 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import android.support.annotation.NonNull;
+import android.support.annotation.Size;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * The {@link Stops} implementation for interval functions
+ *
+ * @param <I> the input type
+ * @param <O> the output type
+ */
+public class IntervalStops<I, O> extends IterableStops<I, O, Stop<I, O>> {
+
+ private final Stop<I, O>[] stops;
+
+ @SafeVarargs
+ public IntervalStops(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ this.stops = stops;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getTypeName() {
+ return "interval";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Map<String, Object> toValueObject() {
+ Map<String, Object> map = super.toValueObject();
+ map.put("stops", toValueObjects(stops));
+ return map;
+ }
+
+ /**
+ * @return an {@link Iterator} for the contained stops
+ */
+ @Override
+ public Iterator<Stop<I, O>> iterator() {
+ return Arrays.asList(stops).iterator();
+ }
+
+ /**
+ * @return The number of contained stops
+ */
+ @Override
+ public int size() {
+ return stops.length;
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IterableStops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IterableStops.java
new file mode 100644
index 0000000000..8c5a6e8913
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IterableStops.java
@@ -0,0 +1,52 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import java.util.Iterator;
+
+/**
+ * Base class for {@link Stops} implementations with a collection of stops
+ *
+ * @param <I> the {@link Stops} input type
+ * @param <O> the {@link Stops} output type
+ * @param <S> the {@link Iterable} element type (usually {@link Stop})
+ */
+public abstract class IterableStops<I, O, S> extends Stops<I, O> implements Iterable<S> {
+
+ /**
+ * @return The size of the contained stops collection
+ */
+ public abstract int size();
+
+ /**
+ * Convenience function to toValueObjects an array of stops to an array of value objects
+ *
+ * @param stops the stops to toValueObjects
+ * @return the stops as value objects
+ */
+ Object[] toValueObjects(Stop<I, O>[] stops) {
+ if (stops != null) {
+ Object[] stopsValue = new Object[stops.length];
+
+ for (int i = 0; i < stopsValue.length; i++) {
+ Stop stop = stops[i];
+ stopsValue[i] = stop.toValueObject();
+ }
+ return stopsValue;
+ }
+
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder buffer = new StringBuilder();
+ Iterator<S> iterator = iterator();
+ while (iterator.hasNext()) {
+ S stop = iterator.next();
+ buffer.append(stop);
+ if (iterator.hasNext()) {
+ buffer.append(",");
+ }
+ }
+ return String.format("%s: [%s]", super.toString(), buffer.toString());
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stop.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stop.java
new file mode 100644
index 0000000000..72164f4c4b
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stop.java
@@ -0,0 +1,109 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import com.mapbox.mapboxsdk.style.functions.Function;
+import com.mapbox.mapboxsdk.style.layers.PropertyValue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A stop represents a certain point in the range of this function
+ *
+ * @param <I> input the stop (function) input type
+ * @param <O> output the stop (function) output type
+ * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#types-function">The style specification</a>
+ */
+public class Stop<I, O> {
+ /**
+ * Creates a {@link Stop} to use in a {@link Function}
+ *
+ * @param in the input for the stop
+ * @param output the output for the stop
+ * @param <I> the input property type
+ * @param <O> the output property type
+ * @return the {@link Stop}
+ */
+ public static <I, O> Stop<I, O> stop(I in, PropertyValue<O> output) {
+ return new Stop<>(in, output.value);
+ }
+
+ /**
+ * Create a composite {@link Stop} for use in a {@link com.mapbox.mapboxsdk.style.functions.CompositeFunction}
+ *
+ * @param zoom the zoom input
+ * @param value the feature property input
+ * @param output the output for the stop
+ * @param <Z> the zoom type
+ * @param <I> the feature property input type
+ * @param <O> the output property type
+ * @return the {@link Stop}
+ * @see Function#composite(String, ExponentialStops)
+ */
+ public static <Z extends Number, I, O> Stop<Stop.CompositeValue<Z, I>, O> stop(Z zoom,
+ I value,
+ PropertyValue<O> output) {
+ return new Stop<>(new Stop.CompositeValue<>(zoom, value), output.value);
+ }
+
+ /**
+ * Represents a composite input value for composite functions (eg zoom and feature property value)
+ *
+ * @param <Z> the zoom input type (typically Float)
+ * @param <V> the feature property input type
+ */
+ public static class CompositeValue<Z extends Number, V> {
+ public final Z zoom;
+ public final V value;
+
+ CompositeValue(Z zoom, V value) {
+ this.zoom = zoom;
+ this.value = value;
+ }
+
+ /**
+ * INTERNAL USAGE ONLY
+ *
+ * @return the value object representation for core conversion
+ */
+ Map<String, Object> toValueObject() {
+ HashMap<String, Object> map = new HashMap<>();
+ map.put("zoom", zoom);
+ map.put("value", value);
+ return map;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[zoom: %s, value: %s]", zoom, value);
+ }
+ }
+
+ /**
+ * The input type
+ */
+ public final I in;
+
+ /**
+ * The output type
+ */
+ public final O out;
+
+ Stop(I in, O out) {
+ this.in = in;
+ this.out = out;
+ }
+
+ /**
+ * INTERNAL USAGE ONLY
+ *
+ * @return an array representation of the Stop
+ */
+ Object[] toValueObject() {
+ return new Object[] {in instanceof CompositeValue ? ((CompositeValue) in).toValueObject() : in, out};
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[%s, %s]", in, out);
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stops.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stops.java
new file mode 100644
index 0000000000..af4f53c072
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/Stops.java
@@ -0,0 +1,99 @@
+package com.mapbox.mapboxsdk.style.functions.stops;
+
+import android.support.annotation.CallSuper;
+import android.support.annotation.NonNull;
+import android.support.annotation.Size;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * The base class for different stops implementations
+ *
+ * @param <I> the input type
+ * @param <O> the output type
+ * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#types-function">The style specification</a>
+ */
+public abstract class Stops<I, O> {
+
+ /**
+ * Convenience method for use in function declarations
+ *
+ * @param stops the collections of discrete stops
+ * @param <I> the Stops input type
+ * @param <O> the Stops output type
+ * @return the {@link Stops} implementation
+ * @see Stop#stop
+ * @see CategoricalStops
+ */
+ @SafeVarargs
+ public static <I, O> CategoricalStops<I, O> categorical(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ return new CategoricalStops<>(stops);
+ }
+
+ /**
+ * Convenience method for use in function declarations
+ *
+ * @param stops the collections of discrete stops
+ * @param <I> the Stops input type
+ * @param <O> the Stops output type
+ * @return the {@link Stops} implementation
+ * @see Stop#stop
+ * @see ExponentialStops
+ */
+ @SafeVarargs
+ public static <I, O> ExponentialStops<I, O> exponential(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ return new ExponentialStops<>(stops);
+ }
+
+ /**
+ * Convenience method for use in function declarations
+ *
+ * @param <T> the Stops input/output type
+ * @return the {@link IdentityStops} implementation
+ * @see Stop#stop
+ * @see IdentityStops
+ */
+ public static <T> IdentityStops<T> identity() {
+ return new IdentityStops<>();
+ }
+
+ /**
+ * Convenience method for use in function declarations
+ *
+ * @param stops the collections of discrete stops
+ * @param <I> the Stops input type
+ * @param <O> the Stops output type
+ * @return the {@link Stops} implementation
+ * @see Stop#stop
+ * @see IntervalStops
+ */
+ @SafeVarargs
+ public static <I, O> IntervalStops<I, O> interval(@NonNull @Size(min = 1) Stop<I, O>... stops) {
+ return new IntervalStops<>(stops);
+ }
+
+ /**
+ * INTERNAL USAGE ONLY
+ *
+ * @return the value object representation for conversion to core
+ */
+ @CallSuper
+ public Map<String, Object> toValueObject() {
+ HashMap<String, Object> map = new HashMap<>();
+ map.put("type", getTypeName());
+ return map;
+ }
+
+ /**
+ * INTERNAL USAGE ONLY
+ *
+ * @return the unique type name as a string according to the style specification
+ */
+ protected abstract String getTypeName();
+
+ @Override
+ public String toString() {
+ return getTypeName();
+ }
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/package-info.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/package-info.java
new file mode 100644
index 0000000000..fa388a9589
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/package-info.java
@@ -0,0 +1,6 @@
+/**
+ * Contains the Mapbox Maps Android Style Function Stop implementation API classes.
+ *
+ * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#types-function">The style specification</a>
+ */
+package com.mapbox.mapboxsdk.style.functions.stops;