SchemaObjectInstance.java

  1. /*
  2.  * Copyright (C) 2021 B3Partners B.V.
  3.  *
  4.  * SPDX-License-Identifier: MIT
  5.  *
  6.  */

  7. package nl.b3p.brmo.schema;

  8. import java.util.Map;
  9. import java.util.SortedSet;
  10. import java.util.TreeSet;
  11. import org.geotools.geometry.jts.WKTWriter2;
  12. import org.locationtech.jts.geom.Geometry;

  13. public class SchemaObjectInstance {
  14.   private final ObjectType objectType;
  15.   private final Map<String, Object> attributes;

  16.   private final WKTWriter2 wktWriter2 = new WKTWriter2();

  17.   public SchemaObjectInstance(ObjectType objectType, Map<String, Object> attributes) {
  18.     this.objectType = objectType;
  19.     this.attributes = attributes;
  20.   }

  21.   public ObjectType getObjectType() {
  22.     return objectType;
  23.   }

  24.   public Map<String, Object> getAttributes() {
  25.     return attributes;
  26.   }

  27.   @Override
  28.   public String toString() {
  29.     StringBuilder s = new StringBuilder(objectType.getName());
  30.     s.append("{");
  31.     SortedSet<String> attributeNames = new TreeSet<>(attributes.keySet());
  32.     boolean first = true;
  33.     for (String name : attributeNames) {
  34.       if (first) {
  35.         first = false;
  36.       } else {
  37.         s.append(", ");
  38.       }
  39.       s.append(name);
  40.       s.append("=");
  41.       Object value = attributes.get(name);
  42.       if (value instanceof Geometry) {
  43.         value = wktWriter2.write((Geometry) value);
  44.       }
  45.       s.append(value);
  46.     }
  47.     s.append("}");
  48.     return s.toString();
  49.   }
  50. }