1 package org.djunits.unit;
2
3 import java.io.Serializable;
4 import java.util.Arrays;
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 import org.djunits.Throw;
9 import org.djunits.unit.quantity.Quantities;
10 import org.djunits.unit.quantity.Quantity;
11 import org.djunits.unit.scale.IdentityScale;
12 import org.djunits.unit.scale.LinearScale;
13 import org.djunits.unit.scale.OffsetLinearScale;
14 import org.djunits.unit.scale.Scale;
15 import org.djunits.unit.si.SIDimensions;
16 import org.djunits.unit.si.SIPrefix;
17 import org.djunits.unit.si.SIPrefixes;
18 import org.djunits.unit.unitsystem.UnitSystem;
19 import org.djunits.unit.util.UnitRuntimeException;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class Unit<U extends Unit<U>> implements Serializable, Cloneable
33 {
34
35 private static final long serialVersionUID = 20190818L;
36
37
38 private String id;
39
40
41 private Set<String> abbreviations;
42
43
44 private String defaultDisplayAbbreviation;
45
46
47 private String defaultTextualAbbreviation;
48
49
50 private String name;
51
52
53 private Scale scale;
54
55
56 private UnitSystem unitSystem;
57
58
59 private boolean generated;
60
61
62 private boolean baseSIUnit;
63
64
65
66
67
68 private Quantity<U> quantity;
69
70
71
72
73
74
75
76 protected Unit()
77 {
78
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 @SuppressWarnings("unchecked")
96 public U build(final Builder<U> builder) throws UnitRuntimeException
97 {
98
99 String cName = getClass().getSimpleName();
100 Throw.whenNull(builder.getId(), "Constructing unit %s: id cannot be null", cName);
101 Throw.when(builder.getId().length() == 0, UnitRuntimeException.class, "Constructing unit %s: id.length cannot be 0",
102 cName);
103 String unitId = builder.getId();
104 Throw.whenNull(builder.getQuantity(), "Constructing unit %s.%s: baseUnit cannot be null", cName, unitId);
105 Throw.whenNull(builder.getName(), "Constructing unit %s.%s: name cannot be null", cName, unitId);
106 Throw.when(builder.getName().length() == 0, UnitRuntimeException.class,
107 "Constructing unit %s.%s: name.length cannot be 0", cName, unitId);
108 Throw.whenNull(builder.getScale(), "Constructing unit %s.%s: scale cannot be null", cName, unitId);
109 Throw.whenNull(builder.getUnitSystem(), "Constructing unit %s.%s: unitSystem cannot be null", cName, unitId);
110
111
112 this.id = unitId;
113 this.name = builder.getName();
114 this.quantity = builder.getQuantity();
115 this.unitSystem = builder.getUnitSystem();
116 this.scale = builder.getScale();
117 this.generated = builder.isGenerated();
118 this.baseSIUnit = this.scale.isBaseSIScale();
119
120
121 if (builder.getDefaultDisplayAbbreviation() == null)
122 {
123 if (builder.getDefaultTextualAbbreviation() == null)
124 {
125 this.defaultDisplayAbbreviation = unitId;
126 }
127 else
128 {
129 this.defaultDisplayAbbreviation = builder.getDefaultTextualAbbreviation();
130 }
131 }
132 else
133 {
134 this.defaultDisplayAbbreviation = builder.getDefaultDisplayAbbreviation();
135 }
136 if (builder.getDefaultTextualAbbreviation() == null)
137 {
138 this.defaultTextualAbbreviation = this.defaultDisplayAbbreviation;
139 }
140 else
141 {
142 this.defaultTextualAbbreviation = builder.getDefaultTextualAbbreviation();
143 }
144 this.abbreviations = new LinkedHashSet<>();
145 this.abbreviations.add(this.defaultDisplayAbbreviation);
146 this.abbreviations.add(this.defaultTextualAbbreviation);
147 this.abbreviations.addAll(builder.getAdditionalAbbreviations());
148
149
150 SIPrefixes siPrefixes = builder.getSiPrefixes() == null ? SIPrefixes.NONE : builder.getSiPrefixes();
151
152
153 this.quantity.registerUnit((U) this, siPrefixes, builder.getSiPrefixPowerFactor());
154 return (U) this;
155 }
156
157
158
159
160
161
162
163
164
165 public U deriveSI(final SIPrefix siPrefix, final double siPrefixPower, final boolean automaticallyGenerated)
166 {
167 Throw.whenNull(siPrefix, "siPrefix cannot be null");
168 try
169 {
170 U clone = clone();
171
172
173 String cloneId = siPrefix.getDefaultTextualPrefix() + clone.getId();
174 String cloneName = clone.getName();
175 if (cloneName.startsWith("square "))
176 {
177 cloneName = "square " + siPrefix.getPrefixName() + cloneName.substring(6);
178 }
179 else if (clone.getName().startsWith("cubic "))
180 {
181 cloneName = "cubic " + siPrefix.getPrefixName() + cloneName.substring(5);
182 }
183 else
184 {
185 cloneName = siPrefix.getPrefixName() + cloneName;
186 }
187 Set<String> cloneAbbreviations = new LinkedHashSet<>();
188 for (String abbreviation : clone.getAbbreviations())
189 {
190 cloneAbbreviations.add(siPrefix.getDefaultTextualPrefix() + abbreviation);
191 }
192 String cloneDefaultAbbreviation = siPrefix.getDefaultDisplayPrefix() + clone.getDefaultDisplayAbbreviation();
193 String cloneDefaultTextualAbbreviation = siPrefix.getDefaultTextualPrefix() + clone.getDefaultTextualAbbreviation();
194
195
196 Builder<U> builder = makeBuilder();
197 builder.setId(cloneId);
198 builder.setName(cloneName);
199 builder.setQuantity(this.quantity);
200 builder.setSiPrefixes(SIPrefixes.NONE, 1.0);
201 builder.setDefaultDisplayAbbreviation(cloneDefaultAbbreviation);
202 builder.setDefaultTextualAbbreviation(cloneDefaultTextualAbbreviation);
203 builder.setAdditionalAbbreviations(cloneAbbreviations.toArray(new String[cloneAbbreviations.size()]));
204 if (getScale() instanceof OffsetLinearScale)
205 {
206 builder.setScale(new OffsetLinearScale(
207 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
208 * ((LinearScale) getScale()).getConversionFactorToStandardUnit(),
209 0.0));
210 }
211 else
212 {
213 builder.setScale(new LinearScale(
214 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
215 * ((LinearScale) getScale()).getConversionFactorToStandardUnit()));
216 }
217 builder.setUnitSystem(this.unitSystem);
218 builder.setGenerated(automaticallyGenerated);
219
220 return clone.build(builder);
221 }
222 catch (CloneNotSupportedException exception)
223 {
224 throw new UnitRuntimeException(exception);
225 }
226 }
227
228
229
230
231
232
233
234
235
236 public U deriveSI(final SIPrefix siPrefix, final double siPrefixPower)
237 {
238 return deriveSI(siPrefix, siPrefixPower, false);
239 }
240
241
242
243
244
245
246
247
248
249
250 public U deriveSIKilo(final SIPrefix siPrefix, final double siPrefixPower, final boolean automaticallyGenerated)
251 {
252 Throw.whenNull(siPrefix, "siPrefix cannot be null");
253 Throw.when(!this.id.startsWith("k"), UnitRuntimeException.class, "deriving from a kilo-unit: id should start with 'k'");
254 Throw.when(!this.defaultTextualAbbreviation.startsWith("k"), UnitRuntimeException.class,
255 "deriving from a kilo-unit: defaultTextualAbbreviation should start with 'k'");
256 Throw.when(!this.defaultDisplayAbbreviation.startsWith("k"), UnitRuntimeException.class,
257 "deriving from a kilo-unit: defaultDisplayAbbreviation should start with 'k'");
258 Throw.when(!this.name.startsWith("kilo"), UnitRuntimeException.class,
259 "deriving from a kilo-unit: name should start with 'kilo'");
260 for (String abbreviation : getAbbreviations())
261 {
262 Throw.when(!abbreviation.startsWith("k"), UnitRuntimeException.class,
263 "deriving from a kilo-unit: each abbreviation should start with 'k'");
264 }
265
266 try
267 {
268 U clone = clone();
269
270
271 String cloneId = siPrefix.getDefaultTextualPrefix() + clone.getId().substring(1);
272 String cloneName = siPrefix.getPrefixName() + clone.getName().substring(4);
273 Set<String> cloneAbbreviations = new LinkedHashSet<>();
274 for (String abbreviation : clone.getAbbreviations())
275 {
276 cloneAbbreviations.add(siPrefix.getDefaultTextualPrefix() + abbreviation.substring(1));
277 }
278 String cloneDefaultAbbreviation =
279 siPrefix.getDefaultDisplayPrefix() + clone.getDefaultDisplayAbbreviation().substring(1);
280 String cloneDefaultTextualAbbreviation =
281 siPrefix.getDefaultTextualPrefix() + clone.getDefaultTextualAbbreviation().substring(1);
282
283
284 Builder<U> builder = makeBuilder();
285 builder.setId(cloneId);
286 builder.setName(cloneName);
287 builder.setQuantity(this.quantity);
288 builder.setSiPrefixes(SIPrefixes.NONE, 1.0);
289 builder.setDefaultDisplayAbbreviation(cloneDefaultAbbreviation);
290 builder.setDefaultTextualAbbreviation(cloneDefaultTextualAbbreviation);
291 builder.setAdditionalAbbreviations(cloneAbbreviations.toArray(new String[cloneAbbreviations.size()]));
292 if (getScale() instanceof OffsetLinearScale)
293 {
294 builder.setScale(new OffsetLinearScale(
295 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
296 * ((LinearScale) getScale()).getConversionFactorToStandardUnit(),
297 0.0));
298 }
299 else
300 {
301 builder.setScale(new LinearScale(
302 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
303 * ((LinearScale) getScale()).getConversionFactorToStandardUnit()));
304 }
305
306 builder.setUnitSystem(this.unitSystem);
307 builder.setGenerated(automaticallyGenerated);
308
309 return clone.build(builder);
310 }
311 catch (CloneNotSupportedException exception)
312 {
313 throw new UnitRuntimeException(exception);
314 }
315 }
316
317
318
319
320
321
322
323
324
325
326 public U derivePerSI(final SIPrefix siPrefix, final double siPrefixPower, final boolean automaticallyGenerated)
327 {
328 Throw.whenNull(siPrefix, "siPrefix cannot be null");
329 try
330 {
331 U clone = clone();
332
333
334 String cloneId = siPrefix.getDefaultTextualPrefix() + clone.getId().replace("1/", "").replace("/", "").trim();
335 String cloneName =
336 siPrefix.getPrefixName() + clone.getName().replace("per", "").replace("1/", "").replace("/", "").trim();
337 Set<String> cloneAbbreviations = new LinkedHashSet<>();
338 for (String abbreviation : clone.getAbbreviations())
339 {
340 cloneAbbreviations
341 .add(siPrefix.getDefaultTextualPrefix() + abbreviation.replace("1/", "").replace("/", "").trim());
342 cloneAbbreviations
343 .add("1" + siPrefix.getDefaultTextualPrefix() + abbreviation.replace("1/", "").replace("/", "").trim());
344 }
345 String cloneDefaultAbbreviation = siPrefix.getDefaultDisplayPrefix()
346 + clone.getDefaultDisplayAbbreviation().replace("1/", "").replace("/", "").trim();
347 String cloneDefaultTextualAbbreviation = siPrefix.getDefaultTextualPrefix()
348 + clone.getDefaultTextualAbbreviation().replace("1/", "").replace("/", "").trim();
349
350
351 Builder<U> builder = makeBuilder();
352 builder.setId(cloneId);
353 builder.setName(cloneName);
354 builder.setQuantity(this.quantity);
355 builder.setSiPrefixes(SIPrefixes.NONE, 1.0);
356 builder.setDefaultDisplayAbbreviation(cloneDefaultAbbreviation);
357 builder.setDefaultTextualAbbreviation(cloneDefaultTextualAbbreviation);
358 builder.setAdditionalAbbreviations(cloneAbbreviations.toArray(new String[cloneAbbreviations.size()]));
359 if (getScale() instanceof OffsetLinearScale)
360 {
361 builder.setScale(new OffsetLinearScale(
362 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
363 * ((LinearScale) getScale()).getConversionFactorToStandardUnit(),
364 0.0));
365 }
366 else
367 {
368 builder.setScale(new LinearScale(
369 (siPrefixPower == 1.0 ? siPrefix.getFactor() : Math.pow(siPrefix.getFactor(), siPrefixPower))
370 * ((LinearScale) getScale()).getConversionFactorToStandardUnit()));
371 }
372 builder.setUnitSystem(this.unitSystem);
373 builder.setGenerated(automaticallyGenerated);
374
375 return clone.build(builder);
376 }
377 catch (CloneNotSupportedException exception)
378 {
379 throw new UnitRuntimeException(exception);
380 }
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 public U deriveLinear(final double scaleFactor, final String derivedId, final String derivedName,
399 final UnitSystem derivedUnitSystem, final String derivedDefaultDisplayAbbreviation,
400 final String derivedDefaultTextualAbbreviation, final String... derivedAbbreviations)
401 {
402 String cName = getClass().getSimpleName();
403 Throw.whenNull(derivedId, "deriving unit from %s.%s; derivedId cannot be null", cName, this.id);
404 Throw.whenNull(derivedName, "deriving unit from %s.%s; derivedName cannot be null", cName, this.id);
405 Throw.whenNull(derivedUnitSystem, "deriving unit from %s.%s; derivedUnitSystem cannot be null", cName, this.id);
406 if (!getScale().getClass().equals(LinearScale.class) && !getScale().getClass().equals(IdentityScale.class))
407 {
408 throw new UnitRuntimeException("Cannot derive from unit " + cName + "." + getId() + " with scale "
409 + getScale().getClass().getSimpleName() + ". Scale should be Linear");
410 }
411
412 try
413 {
414 U clone = clone();
415
416
417 Builder<U> builder = makeBuilder();
418 builder.setId(derivedId);
419 builder.setName(derivedName);
420 builder.setQuantity(this.quantity);
421 builder.setSiPrefixes(SIPrefixes.NONE, 1.0);
422 builder.setScale(new LinearScaleg/djunits/unit/scale/LinearScale.html#LinearScale">LinearScale(scaleFactor * ((LinearScale) getScale()).getConversionFactorToStandardUnit()));
423 builder.setUnitSystem(derivedUnitSystem);
424 builder.setDefaultDisplayAbbreviation(derivedDefaultDisplayAbbreviation);
425 builder.setDefaultTextualAbbreviation(derivedDefaultTextualAbbreviation);
426 if (derivedAbbreviations != null)
427 {
428 builder.setAdditionalAbbreviations(derivedAbbreviations);
429 }
430
431 return clone.build(builder);
432 }
433 catch (CloneNotSupportedException exception)
434 {
435 throw new UnitRuntimeException(exception);
436 }
437 }
438
439
440
441
442
443
444
445
446
447
448
449 public U deriveLinear(final double scaleFactor, final String derivedId, final String derivedName,
450 final UnitSystem derivedUnitSystem)
451 {
452 return deriveLinear(scaleFactor, derivedId, derivedName, derivedUnitSystem, null, null);
453 }
454
455
456
457
458
459
460
461
462
463
464 public U deriveLinear(final double scaleFactor, final String derivedId, final String derivedName)
465 {
466 return deriveLinear(scaleFactor, derivedId, derivedName, getUnitSystem());
467 }
468
469
470
471
472
473 public Builder<U> makeBuilder()
474 {
475 return new Builder<U>();
476 }
477
478
479
480
481
482
483 @SuppressWarnings("unchecked")
484 public static SIUnit lookupOrCreateUnitWithSIDimensions(final SIDimensions siDimensions)
485 {
486 Throw.whenNull(siDimensions, "siDimensions cannot be null");
487
488 Quantity<SIUnit> quantity = null;
489 SIUnit unit = null;
490
491 Set<Quantity<?>> baseUnitSet = Quantities.INSTANCE.getQuantities(siDimensions);
492 for (Quantity<?> bu : baseUnitSet)
493 {
494 if (bu.getStandardUnit().getClass().equals(Unit.class))
495 {
496 quantity = (Quantity<SIUnit>) bu;
497 }
498 }
499
500 if (quantity == null)
501 {
502 quantity = new Quantity<SIUnit>(siDimensions.toString(), siDimensions);
503 Builder<SIUnit> builder = new Builder<>();
504 builder.setId(siDimensions.toString(true, true));
505 builder.setName(siDimensions.toString(true, true));
506 builder.setQuantity(quantity);
507 builder.setScale(IdentityScale.SCALE);
508 builder.setGenerated(true);
509 builder.setUnitSystem(UnitSystem.SI_DERIVED);
510 builder.setSiPrefixes(SIPrefixes.NONE, 1.0);
511 unit = new SIUnit();
512 unit.build(builder);
513 }
514 else
515 {
516 unit = quantity.getStandardUnit();
517 }
518
519 return unit;
520 }
521
522
523
524
525
526 public String getId()
527 {
528 return this.id;
529 }
530
531
532
533
534
535 public Set<String> getAbbreviations()
536 {
537 return new LinkedHashSet<>(this.abbreviations);
538 }
539
540
541
542
543
544 public String getDefaultDisplayAbbreviation()
545 {
546 return this.defaultDisplayAbbreviation;
547 }
548
549
550
551
552
553 public String getDefaultTextualAbbreviation()
554 {
555 return this.defaultTextualAbbreviation;
556 }
557
558
559
560
561
562 public String getName()
563 {
564 return this.name;
565 }
566
567
568
569
570
571 public Scale getScale()
572 {
573 return this.scale;
574 }
575
576
577
578
579
580 public UnitSystem getUnitSystem()
581 {
582 return this.unitSystem;
583 }
584
585
586
587
588
589
590 public Quantity<U> getQuantity()
591 {
592 return this.quantity;
593 }
594
595
596
597
598
599 public boolean isGenerated()
600 {
601 return this.generated;
602 }
603
604
605
606
607
608
609 public boolean isBaseSIUnit()
610 {
611 return this.baseSIUnit;
612 }
613
614
615
616
617
618 public U getStandardUnit()
619 {
620 return getQuantity().getStandardUnit();
621 }
622
623
624 @SuppressWarnings("unchecked")
625 @Override
626 public U clone() throws CloneNotSupportedException
627 {
628 return (U) super.clone();
629 }
630
631
632 @Override
633 public int hashCode()
634 {
635 final int prime = 31;
636 int result = 1;
637 result = prime * result + ((this.abbreviations == null) ? 0 : this.abbreviations.hashCode());
638 result = prime * result + ((this.quantity == null) ? 0 : this.quantity.hashCode());
639 result = prime * result + ((this.defaultDisplayAbbreviation == null) ? 0 : this.defaultDisplayAbbreviation.hashCode());
640 result = prime * result + ((this.defaultTextualAbbreviation == null) ? 0 : this.defaultTextualAbbreviation.hashCode());
641 result = prime * result + (this.generated ? 1231 : 1237);
642 result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
643 result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
644 result = prime * result + ((this.scale == null) ? 0 : this.scale.hashCode());
645 result = prime * result + ((this.unitSystem == null) ? 0 : this.unitSystem.hashCode());
646 return result;
647 }
648
649
650 @Override
651 @SuppressWarnings("checkstyle:needbraces")
652 public boolean equals(final Object obj)
653 {
654 if (this == obj)
655 return true;
656 if (obj == null)
657 return false;
658 if (getClass() != obj.getClass())
659 return false;
660 Unit<?> other = (Unit<?>) obj;
661 if (this.abbreviations == null)
662 {
663 if (other.abbreviations != null)
664 return false;
665 }
666 else if (!this.abbreviations.equals(other.abbreviations))
667 return false;
668 if (this.quantity == null)
669 {
670 if (other.quantity != null)
671 return false;
672 }
673 else if (!this.quantity.equals(other.quantity))
674 return false;
675 if (this.defaultDisplayAbbreviation == null)
676 {
677 if (other.defaultDisplayAbbreviation != null)
678 return false;
679 }
680 else if (!this.defaultDisplayAbbreviation.equals(other.defaultDisplayAbbreviation))
681 return false;
682 if (this.defaultTextualAbbreviation == null)
683 {
684 if (other.defaultTextualAbbreviation != null)
685 return false;
686 }
687 else if (!this.defaultTextualAbbreviation.equals(other.defaultTextualAbbreviation))
688 return false;
689 if (this.generated != other.generated)
690 return false;
691 if (this.id == null)
692 {
693 if (other.id != null)
694 return false;
695 }
696 else if (!this.id.equals(other.id))
697 return false;
698 if (this.name == null)
699 {
700 if (other.name != null)
701 return false;
702 }
703 else if (!this.name.equals(other.name))
704 return false;
705 if (this.scale == null)
706 {
707 if (other.scale != null)
708 return false;
709 }
710 else if (!this.scale.equals(other.scale))
711 return false;
712 if (this.unitSystem == null)
713 {
714 if (other.unitSystem != null)
715 return false;
716 }
717 else if (!this.unitSystem.equals(other.unitSystem))
718 return false;
719 return true;
720 }
721
722
723 @Override
724 public String toString()
725 {
726 return this.defaultDisplayAbbreviation;
727 }
728
729
730
731
732
733
734
735
736
737
738
739 public static class Builder<U extends Unit<U>> implements Serializable
740 {
741
742 private static final long serialVersionUID = 20191018L;
743
744
745 private String id;
746
747
748 private Set<String> additionalAbbreviations = new LinkedHashSet<>();
749
750
751 private String defaultDisplayAbbreviation;
752
753
754 private String defaultTextualAbbreviation;
755
756
757 private String name;
758
759
760 private Scale scale;
761
762
763 private UnitSystem unitSystem;
764
765
766 private SIPrefixes siPrefixes;
767
768
769 private double siPrefixPower = 1.0;
770
771
772 private boolean generated = false;
773
774
775
776
777
778 private Quantity<U> quantity;
779
780
781
782
783
784 public Builder()
785 {
786
787 }
788
789
790
791
792
793 public SIPrefixes getSiPrefixes()
794 {
795 return this.siPrefixes;
796 }
797
798
799
800
801
802 public double getSiPrefixPowerFactor()
803 {
804 return this.siPrefixPower;
805 }
806
807
808
809
810
811
812
813
814
815 public Builder<U> setSiPrefixes(final SIPrefixes newSiPrefixes, final double power)
816 {
817 this.siPrefixes = newSiPrefixes;
818 this.siPrefixPower = power;
819 return this;
820 }
821
822
823
824
825
826 public String getId()
827 {
828 return this.id;
829 }
830
831
832
833
834
835
836
837 public Builder<U> setId(final String newId)
838 {
839 this.id = newId;
840 return this;
841 }
842
843
844
845
846
847 public Set<String> getAdditionalAbbreviations()
848 {
849 return this.additionalAbbreviations;
850 }
851
852
853
854
855
856
857 public Builder<U> setAdditionalAbbreviations(final String... newAdditionalAbbreviations)
858 {
859 this.additionalAbbreviations = new LinkedHashSet<>(Arrays.asList(newAdditionalAbbreviations));
860 return this;
861 }
862
863
864
865
866
867 public String getDefaultDisplayAbbreviation()
868 {
869 return this.defaultDisplayAbbreviation;
870 }
871
872
873
874
875
876
877 public Builder<U> setDefaultDisplayAbbreviation(final String newDefaultDisplayAbbreviation)
878 {
879 this.defaultDisplayAbbreviation = newDefaultDisplayAbbreviation;
880 return this;
881 }
882
883
884
885
886
887 public String getDefaultTextualAbbreviation()
888 {
889 return this.defaultTextualAbbreviation;
890 }
891
892
893
894
895
896
897 public Builder<U> setDefaultTextualAbbreviation(final String newDefaultTextualAbbreviation)
898 {
899 this.defaultTextualAbbreviation = newDefaultTextualAbbreviation;
900 return this;
901 }
902
903
904
905
906
907 public String getName()
908 {
909 return this.name;
910 }
911
912
913
914
915
916
917 public Builder<U> setName(final String newName)
918 {
919 this.name = newName;
920 return this;
921 }
922
923
924
925
926
927 public Scale getScale()
928 {
929 return this.scale;
930 }
931
932
933
934
935
936
937 public Builder<U> setScale(final Scale newScale)
938 {
939 this.scale = newScale;
940 return this;
941 }
942
943
944
945
946
947 public UnitSystem getUnitSystem()
948 {
949 return this.unitSystem;
950 }
951
952
953
954
955
956
957 public Builder<U> setUnitSystem(final UnitSystem newUnitSystem)
958 {
959 this.unitSystem = newUnitSystem;
960 return this;
961 }
962
963
964
965
966
967 public boolean isGenerated()
968 {
969 return this.generated;
970 }
971
972
973
974
975
976
977 public Builder<U> setGenerated(final boolean newGenerated)
978 {
979 this.generated = newGenerated;
980 return this;
981 }
982
983
984
985
986
987 public Quantity<U> getQuantity()
988 {
989 return this.quantity;
990 }
991
992
993
994
995
996
997 public Builder<U> setQuantity(final Quantity<U> newQuantity)
998 {
999 this.quantity = newQuantity;
1000 return this;
1001 }
1002
1003
1004 @Override
1005 public String toString()
1006 {
1007 return "Builder [id=" + this.id + ", name=" + this.name + ", scale=" + this.scale + "]";
1008 }
1009
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019 public static SIUnit getUnit(final String unitString)
1020 {
1021 Throw.whenNull(unitString, "Error parsing SIVector: unitString is null");
1022 Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing SIVector: empty unitString");
1023 try
1024 {
1025 SIUnit unit = Unit.lookupOrCreateUnitWithSIDimensions(SIDimensions.of(unitString));
1026 if (unit != null)
1027 {
1028 return unit;
1029 }
1030 }
1031 catch (Exception exception)
1032 {
1033 throw new IllegalArgumentException("Error parsing SIUnit from " + unitString, exception);
1034 }
1035 throw new IllegalArgumentException("Error parsing SIVector with unit " + unitString);
1036 }
1037
1038 }