1 /*
2 * MoneyWise: Finance Application
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.moneywise.data.basic;
18
19 import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
20
21 /**
22 * Asset Direction.
23 */
24 public enum MoneyWiseAssetDirection {
25 /**
26 * To.
27 */
28 TO(1),
29
30 /**
31 * From.
32 */
33 FROM(2);
34
35 /**
36 * The String name.
37 */
38 private String theName;
39
40 /**
41 * Class Id.
42 */
43 private final int theId;
44
45 /**
46 * Constructor.
47 *
48 * @param uId the Id
49 */
50 MoneyWiseAssetDirection(final int uId) {
51 theId = uId;
52 }
53
54 /**
55 * Obtain class Id.
56 *
57 * @return the Id
58 */
59 public int getId() {
60 return theId;
61 }
62
63 @Override
64 public String toString() {
65 /* If we have not yet loaded the name */
66 if (theName == null) {
67 /* Load the name */
68 theName = bundleIdForAssetDirection(this).getValue();
69 }
70
71 /* return the name */
72 return theName;
73 }
74
75 /**
76 * Reverse.
77 *
78 * @return the reversed direction
79 */
80 public MoneyWiseAssetDirection reverse() {
81 return this == TO
82 ? FROM
83 : TO;
84 }
85
86 /**
87 * Is this the from direction?
88 *
89 * @return true/false
90 */
91 public boolean isFrom() {
92 return this == FROM;
93 }
94
95 /**
96 * Is this the to direction?
97 *
98 * @return true/false
99 */
100 public boolean isTo() {
101 return this == TO;
102 }
103
104 /**
105 * get value from name.
106 *
107 * @param pName the name value
108 * @return the corresponding enum object
109 */
110 public static MoneyWiseAssetDirection fromName(final String pName) {
111 for (MoneyWiseAssetDirection myDir : values()) {
112 if (pName.equals(myDir.toString())) {
113 return myDir;
114 }
115 }
116 return null;
117 }
118
119 /**
120 * Obtain the resource bundleId for the assetDirection.
121 *
122 * @param pDirection the direction
123 * @return the resource bundleId
124 */
125 private static OceanusBundleId bundleIdForAssetDirection(final MoneyWiseAssetDirection pDirection) {
126 /* Create the map and return it */
127 return switch (pDirection) {
128 case TO -> MoneyWiseBasicResource.ASSETDIRECTION_TO;
129 case FROM -> MoneyWiseBasicResource.ASSETDIRECTION_FROM;
130 default -> throw new IllegalArgumentException();
131 };
132 }
133 }