summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/functions/stops/IterableStops.java
blob: 8c5a6e89136127035feb5b4a6ca6aa7a192a66ce (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
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());
  }
}