View Javadoc
1   /*
2    * Copyright (C) 2022-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values;
5   
6   
7   import java.util.Arrays;
8   import java.util.Objects;
9   
10  
11  /**
12   * Address with wgs84 position.
13   *
14   * DSGVO relevant.
15   *
16   * TODO compareTo(): AddressWithWGS84Position
17   */
18  public final class AddressWithWGS84Position extends Address
19   {
20    /* *
21     * Cache for singletons.
22     */
23    // private static final Map<NTuple16<Country, PostalCode, City, Province, District, Street, BuildingNr, BuildingName, SubBuilding, PoBoxNumber, Department, Neighbourhood, Block, BFPONumber, Lines, WGS84Position>, AddressWithWGS84Position> CACHE = new WeakHashMap<>();
24  
25    /**
26     * WGS84 position.
27     */
28    private final WGS84Position position;
29  
30  
31    /**
32     * Constructor.
33     *
34     * @param country Country
35     * @param postalCode Postal code
36     * @param city City
37     * @param province Province
38     * @param district District
39     * @param street Street
40     * @param buildingNr Bulding number
41     * @param buildingName Building name
42     * @param subBuilding Sub building
43     * @param poBoxNumber Post office box number
44     * @param department Department
45     * @param neighbourhood Neighbourhood
46     * @param block Block
47     * @param bFPONumber British Forces Post Office Number
48     * @param lines Lines 1-5
49     * @param position WGS84Position
50     */
51    private AddressWithWGS84Position(final Country country, final PostalCode postalCode, final City city, final Province province, final District district, final Street street, final BuildingNr buildingNr, final BuildingName buildingName, final SubBuilding subBuilding, final PoBoxNumber poBoxNumber, final Department department, final Neighbourhood neighbourhood, final Block block, final BFPONumber bFPONumber, final Lines lines, final WGS84Position position)
52     {
53      super(country, postalCode, city, province, district, street, buildingNr, buildingName, subBuilding, poBoxNumber, department, neighbourhood, block, bFPONumber, lines);
54      Objects.requireNonNull(position, "position"); //$NON-NLS-1$
55      this.position = position;
56     }
57  
58  
59    /**
60     * AddressWithWGS84Position factory.
61     *
62     * @param country Country
63     * @param postalCode Postal code
64     * @param city City
65     * @param province Province
66     * @param district District
67     * @param street Street
68     * @param buildingNr Bulding number
69     * @param buildingName Building name
70     * @param subBuilding Sub building
71     * @param poBoxNumber Post office box number
72     * @param department Department
73     * @param neighbourhood Neighbourhood
74     * @param block Block
75     * @param bFPONumber British Forces Post Office Number
76     * @param lines Lines 1-5
77     * @param position WGS84Position
78     * @return AddressWithWGS84Position object
79     */
80    public static AddressWithWGS84Position of(final Country country, final PostalCode postalCode, final City city, final Province province, final District district, final Street street, final BuildingNr buildingNr, final BuildingName buildingName, final SubBuilding subBuilding, final PoBoxNumber poBoxNumber, final Department department, final Neighbourhood neighbourhood, final Block block, final BFPONumber bFPONumber, final Lines lines, final WGS84Position position)
81     {
82      /*
83      final NTuple16<Country, PostalCode, City, Province, District, Street, BuildingNr, BuildingName, SubBuilding, PoBoxNumber, Department, Neighbourhood, Block, BFPONumber, Lines, WGS84Position> tuple = NTuple16.of(country, postalCode, city, province, district, street, buildingNr, buildingName, subBuilding, poBoxNumber, department, neighbourhood, block, bFPONumber, lines, position);
84      synchronized (AddressWithWGS84Position.class)
85       {
86        AddressWithWGS84Position obj = AddressWithWGS84Position.CACHE.get(tuple);
87        if (obj != null)
88         {
89          return obj;
90         }
91        obj = new AddressWithWGS84Position(country, postalCode, city, province, district, street, buildingNr, buildingName, subBuilding, poBoxNumber, department, neighbourhood, block, bFPONumber, lines, position);
92        AddressWithWGS84Position.CACHE.put(tuple, obj);
93        return obj;
94       }
95      */
96      return new AddressWithWGS84Position(country, postalCode, city, province, district, street, buildingNr, buildingName, subBuilding, poBoxNumber, department, neighbourhood, block, bFPONumber, lines, position);
97     }
98  
99  
100   /**
101    * AddressWithWGS84Position factory.
102    *
103    * @param value country,postalcode,city,province,district,street,buildingnr,buildingname,subbuilding,poboxnumber,department,neighbourhood,block,bfponumber,lines,latitude longitude altitude
104    * @return AddressWithWGS84Position object
105    */
106   public static AddressWithWGS84Position of(final String value)
107    {
108     String[] values = value.split(",");
109     if ((values.length < 1) || (values.length > 16))
110      {
111       throw new IllegalArgumentException("value not in expected format: " + values.length);
112      }
113     if (values.length < 16)
114      {
115       values = Arrays.copyOf(values, 16);
116       for (int i = 1; i < 16; ++i)
117        {
118         if (values[i] == null)
119          {
120           values[i] = "";
121          }
122        }
123      }
124     final var country = Country.of(values[0]);
125     final PostalCode postalCode = values[1].isEmpty() ? null : PostalCode.of(values[1]);
126     final City city = values[2].isEmpty() ? null : City.of(values[2]);
127     final Province province = values[3].isEmpty() ? null : Province.of(values[3]);
128     final District district = values[4].isEmpty() ? null : District.of(values[4]);
129     final Street street = values[5].isEmpty() ? null : Street.of(values[5]);
130     final BuildingNr buildingNr = values[6].isEmpty() ? null : BuildingNr.of(values[6]);
131     final BuildingName buildingName = values[7].isEmpty() ? null : BuildingName.of(values[7]);
132     final SubBuilding subBuilding = values[8].isEmpty() ? null : SubBuilding.of(values[8]);
133     final PoBoxNumber poBoxNumber = values[9].isEmpty() ? null : PoBoxNumber.of(values[9]);
134     final Department department = values[10].isEmpty() ? null : Department.of(values[10]);
135     final Neighbourhood neighbourhood = values[11].isEmpty() ? null : Neighbourhood.of(values[11]);
136     final Block block = values[12].isEmpty() ? null : Block.of(values[12]);
137     final BFPONumber bFPONumber = values[13].isEmpty() ? null : BFPONumber.of(values[13]);
138     final Lines lines = values[14].isEmpty() ? null : Lines.of(values[14]);
139     final WGS84Position position = values[15].isEmpty() ? null : WGS84Position.of(values[15]);
140     return of(country, postalCode, city, province, district, street, buildingNr, buildingName, subBuilding, poBoxNumber, department, neighbourhood, block, bFPONumber, lines, position);
141    }
142 
143 
144   /**
145    * Get position.
146    *
147    * @return The position
148    */
149   public WGS84Position getPosition()
150    {
151     return this.position;
152    }
153 
154 
155   /**
156    * Returns the value of this AddressWithWGS84Position as a string.
157    *
158    * @return The text value represented by this object after conversion to type string.
159    */
160   @Override
161   public String stringValue()
162    {
163     return super.stringValue() + this.position.stringValue();
164    }
165 
166 
167   /**
168    * Calculate hash code.
169    *
170    * @return Hash
171    * @see java.lang.Object#hashCode()
172    */
173   @Override
174   public int hashCode()
175    {
176     return Objects.hash(super.hashCode(), this.position);
177    }
178 
179 
180   /**
181    * Is equal with another object.
182    *
183    * @param obj Object
184    * @return true when equal, false otherwise
185    * @see java.lang.Object#equals(java.lang.Object)
186    */
187   @Override
188   public boolean equals(final Object obj)
189    {
190     if (this == obj)
191      {
192       return true;
193      }
194     // if ((obj == null) || (this.getClass() != obj.getClass()))
195     if (!(obj instanceof AddressWithWGS84Position))
196      {
197       return false;
198      }
199     final AddressWithWGS84Position other = (AddressWithWGS84Position)obj;
200     boolean result = this.position.equals(other.position);
201     if (result)
202      {
203       result = super.equals(other);
204      }
205     return result;
206    }
207 
208 
209   /**
210    * Returns the string representation of this AddressWithWGS84Position.
211    *
212    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
213    *
214    * "AddressWithWGS84Position[]"
215    *
216    * @return String representation of this AddressWithWGS84Position
217    * @see java.lang.Object#toString()
218    */
219   @SuppressWarnings({"checkstyle:NoWhitespaceBefore", "checkstyle:SeparatorWrap"})
220   @Override
221   public String toString()
222    {
223     final var builder = new StringBuilder(182);
224     builder.append("AddressWithWGS84Position[position=").append(this.position) //$NON-NLS-1$
225       .append(", ").append(super.toString()) //$NON-NLS-1$
226       .append(']');
227     return builder.toString();
228    }
229 
230  }