daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest May 27th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY;
  2. import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;
  3. import static jsinterop.annotations.JsPackage.GLOBAL;
  4.  
  5. import com.fasterxml.jackson.annotation.JsonIgnore;
  6. import com.fasterxml.jackson.annotation.JsonSubTypes;
  7. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  8. import com.fasterxml.jackson.annotation.JsonTypeName;
  9. import javax.annotation.Nullable;
  10. import jsinterop.annotations.JsOverlay;
  11. import jsinterop.annotations.JsType;
  12.  
  13. @JsonTypeInfo(use = NAME, include = EXISTING_PROPERTY, property = "type", visible = true)
  14. @JsonSubTypes({
  15.         @JsonSubTypes.Type(value = GeoJson.Feature.class, name = "Feature"),
  16.         @JsonSubTypes.Type(value = GeoJson.FeatureCollection.class, name = "FeatureCollection")
  17. })
  18. @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  19. class GeoJson {
  20.     public String type;
  21.     public final @JsOverlay @JsonIgnore
  22.     Type getTypeEnum() { return Type.valueOf(type); }
  23.     public final @JsOverlay @JsonIgnore void setTypeEnum(Type type) { this.type = type.name(); }
  24.  
  25.     public static @JsOverlay FeatureCollection featureCollection(Feature... features) {
  26.         FeatureCollection o = new FeatureCollection();
  27.         o.setTypeEnum(Type.FeatureCollection);
  28.         o.features = features;
  29.         return o;
  30.     }
  31.  
  32.     public static @JsOverlay Feature feature(Geometry geometry) { return feature(null, geometry); }
  33.     public static @JsOverlay Feature feature(@Nullable String featureId, Geometry geometry) {
  34.         Feature o = new Feature();
  35.         o.setTypeEnum(Type.Feature);
  36.         o.id = featureId;
  37.         o.geometry = geometry;
  38.         return o;
  39.     }
  40.  
  41.     public static @JsOverlay Point point(double x, double y) { return point(new double[] { x, y }); }
  42.     public static @JsOverlay Point point(double[] coordinates) {
  43.         Point o = new Point();
  44.         o.setTypeEnum(Geometry.Type.Point);
  45.         o.coordinates = coordinates;
  46.         return o;
  47.     }
  48.  
  49.     public static @JsOverlay Polygon polygon(double[][] coordinates) {
  50.         Polygon o = new Polygon();
  51.         o.setTypeEnum(Geometry.Type.Polygon);
  52.         o.coordinates = new double[][][] { coordinates };
  53.         return o;
  54.     }
  55.  
  56.     public enum Type {Feature, FeatureCollection}
  57.  
  58.     @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  59.     public static final class Feature extends GeoJson {
  60.         public @Nullable String id;
  61.         public Geometry geometry;
  62.     }
  63.  
  64.     @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  65.     public static class FeatureCollection extends GeoJson {
  66.         public Feature[] features;
  67.     }
  68.  
  69.     @JsonTypeInfo(use = NAME, include = EXISTING_PROPERTY, property = "type", visible = true)
  70.     @JsonSubTypes({
  71.             @JsonSubTypes.Type(value = Point.class, name = "Point"),
  72.             @JsonSubTypes.Type(value = Polygon.class, name = "Polygon")
  73.     })
  74.     @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  75.     public static abstract class Geometry {
  76.         public String type;
  77.         public final @JsOverlay @JsonIgnore Geometry.Type getTypeEnum() { return Geometry.Type.valueOf(type); }
  78.         public final @JsOverlay @JsonIgnore void setTypeEnum(Geometry.Type type) { this.type = type.name(); }
  79.  
  80.         public final @JsOverlay <T> T accept(GeometryVisitor<T> fn) { switch (getTypeEnum()) {
  81.             case Point: return fn.point((Point) this);
  82.             case Polygon: return fn.polygon((Polygon) this);
  83.             default: throw new UnsupportedOperationException("unexpected type " + type);
  84.         } }
  85.  
  86.         public static @JsOverlay @Nullable Point isPoint(@Nullable Geometry g) {
  87.             return g == null ? null : g.accept(new GeometryVisitor<Point>() {
  88.                 @Override public Point point(Point g) { return g; }
  89.                 @Override public Point polygon(Polygon p) { return null; }
  90.             });
  91.         }
  92.  
  93.         public static @JsOverlay @Nullable Polygon isPolygon(@Nullable Geometry g) {
  94.             return g == null ? null : g.accept(new GeometryVisitor<Polygon>() {
  95.                 @Override public Polygon point(Point g) { return null; }
  96.                 @Override public Polygon polygon(Polygon p) { return p; }
  97.             });
  98.         }
  99.  
  100.         public enum Type {Point, Polygon}
  101.     }
  102.  
  103.     @JsonTypeName("Point") @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  104.     public static class Point extends Geometry {
  105.         public double[] coordinates;
  106.         public final @JsOverlay @JsonIgnore double x() { return coordinates[0]; }
  107.         public final @JsOverlay @JsonIgnore double y() { return coordinates[1]; }
  108.     }
  109.  
  110.     @JsonTypeName("Polygon") @JsType(namespace = GLOBAL, name = "Object", isNative = true)
  111.     public static final class Polygon extends Geometry {
  112.         public double[][][] coordinates;
  113.         public final @JsOverlay @JsonIgnore double[][] shell() { return coordinates[0]; }
  114.     }
  115.  
  116.     public interface GeometryVisitor<T> {
  117.         T point(Point g);
  118.         T polygon(Polygon p);
  119.     }
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top