1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.item.atomic.impl;
7   
8   import dev.metaschema.core.datatype.IDataTypeAdapter;
9   import dev.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider;
10  import dev.metaschema.core.metapath.impl.AbstractMapKey;
11  import dev.metaschema.core.metapath.item.atomic.AbstractAtomicItemBase;
12  import dev.metaschema.core.metapath.item.atomic.IBooleanItem;
13  import dev.metaschema.core.metapath.item.atomic.IStringItem;
14  import dev.metaschema.core.metapath.item.function.IMapKey;
15  import dev.metaschema.core.metapath.item.function.IOpaqueMapKey;
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  
18  /**
19   * An implementation of a Metapath atomic item with a boolean value.
20   */
21  public class BooleanItemImpl
22      extends AbstractAtomicItemBase<Boolean>
23      implements IBooleanItem {
24    @NonNull
25    private static final String TRUE_STRING = "true";
26    @NonNull
27    private static final String FALSE_STRING = "false";
28    @NonNull
29    private static final IStringItem TRUE_STRING_ITEM = IStringItem.valueOf(TRUE_STRING);
30    @NonNull
31    private static final IStringItem FALSE_STRING_ITEM = IStringItem.valueOf(FALSE_STRING);
32  
33    private final boolean booleanValue;
34  
35    /**
36     * Construct a new item with the provided {@code value}.
37     *
38     * @param value
39     *          the value to wrap
40     */
41    public BooleanItemImpl(boolean value) {
42      this.booleanValue = value;
43    }
44  
45    @Override
46    public Boolean getValue() {
47      return toBoolean();
48    }
49  
50    @Override
51    public boolean toBoolean() {
52      return booleanValue;
53    }
54  
55    @Override
56    public String asString() {
57      return toBoolean() ? TRUE_STRING : FALSE_STRING;
58    }
59  
60    @Override
61    public IStringItem asStringItem() {
62      return toBoolean() ? TRUE_STRING_ITEM : FALSE_STRING_ITEM;
63    }
64  
65    @Override
66    public IDataTypeAdapter<Boolean> getJavaTypeAdapter() {
67      return MetaschemaDataTypeProvider.BOOLEAN;
68    }
69  
70    @Override
71    protected String getValueSignature() {
72      return asString();
73    }
74  
75    @Override
76    public IMapKey asMapKey() {
77      return new MapKey();
78    }
79  
80    @Override
81    public int hashCode() {
82      return Boolean.hashCode(booleanValue);
83    }
84  
85    @Override
86    public boolean equals(Object obj) {
87      return this == obj
88          || obj instanceof IBooleanItem && compareTo((IBooleanItem) obj) == 0;
89    }
90  
91    private final class MapKey
92        extends AbstractMapKey
93        implements IOpaqueMapKey {
94      @Override
95      public IBooleanItem getKey() {
96        return BooleanItemImpl.this;
97      }
98    }
99  }