Cherry pick String off-loading improvements from `wip-nanocoatexec`.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / JavaMEConfiguration.java
blobd5c4d91f370f06cf72a56005f03ff94a5b1771be
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.swm;
12 import cc.squirreljme.plugin.util.StringUtils;
13 import java.lang.ref.Reference;
14 import java.lang.ref.WeakReference;
15 import java.util.Objects;
17 /**
18 * This represents a configuration such as CLDC which specifies which base
19 * classes are available. Configurations may optionally be "compact" in which
20 * they are a lighter version.
22 * @since 2016/12/14
24 public final class JavaMEConfiguration
25 implements Comparable<JavaMEConfiguration>, MarkedDependency, MarkedProvided
27 /** CDC Application. */
28 public static final APIName CDC_NAME =
29 new APIName("CDC");
31 /** CLDC Application. */
32 public static final APIName CLDC_NAME =
33 new APIName("CLDC");
35 /** Name. */
36 protected final APIName name;
38 /** Version. */
39 protected final SuiteVersion version;
41 /** Is this configuration compact? */
42 protected final boolean compact;
44 /** String representation. */
45 private Reference<String> _string;
47 /**
48 * Initializes the configuration using the given API name and version.
50 * @param __n The name to use.
51 * @param __v The version of the suite.
52 * @param __c If {@code true} then the configuration is compact.
53 * @throws NullPointerException On null arguments.
54 * @since 2017/11/30
56 public JavaMEConfiguration(APIName __n, SuiteVersion __v, boolean __c)
57 throws NullPointerException
59 if (__n == null || __v == null)
60 throw new NullPointerException("NARG");
62 // Set
63 this.name = __n;
64 this.version = __v;
65 this.compact = __c;
68 /**
69 * Initializes the configuration by parsing the given string.
71 * @param __n The string to parse.
72 * @throws NullPointerException On null arguments.
73 * @since 2017/11/30
75 public JavaMEConfiguration(String __n)
76 throws NullPointerException
78 if (__n == null)
79 throw new NullPointerException("NARG");
81 /* {@squirreljme.error DG02 Expected two or three fields for the
82 configuration. (The input string)} */
83 String[] fields = StringUtils.fieldSplit('-', __n);
84 int fn = fields.length;
85 if (fn != 2 && fn != 3)
86 throw new InvalidSuiteException(String.format("AR02 %s", __n));
88 // Potentially compact?
89 this.compact = (fn > 2 &&
90 0 == fields[2].compareToIgnoreCase("compact"));
92 // Parse name and version
93 this.name = new APIName(fields[0]);
94 this.version = new SuiteVersion(fields[1]);
97 /**
98 * {@inheritDoc}
99 * @since 2017/11/30
101 @Override
102 public int compareTo(JavaMEConfiguration __o)
104 APIName aName = this.name;
105 APIName bName = __o.name;
107 // CDC is always better than CLDC
108 if (aName.equals(JavaMEConfiguration.CDC_NAME) &&
109 !bName.equals(JavaMEConfiguration.CDC_NAME))
110 return 1;
112 int rv = aName.compareTo(bName);
113 if (rv != 0)
114 return rv;
116 rv = this.version.compareTo(__o.version);
117 if (rv != 0)
118 return rv;
120 // Compact is before non-compact
121 boolean a = this.compact,
122 b = __o.compact;
123 if (a != b)
124 return (a ? -1 : 1);
125 return 0;
129 * {@inheritDoc}
130 * @since 2017/11/30
132 @Override
133 public boolean equals(Object __o)
135 if (this == __o)
136 return true;
138 if (!(__o instanceof JavaMEConfiguration))
139 return false;
141 JavaMEConfiguration o = (JavaMEConfiguration)__o;
142 return this.name.equals(o.name) &&
143 this.version.equals(o.version) &&
144 this.compact == o.compact;
148 * {@inheritDoc}
149 * @since 2017/11/30
151 @Override
152 public int hashCode()
154 return this.name.hashCode() ^
155 Objects.hashCode(this.version) ^
156 (this.compact ? 0xFFFFFFFF : 0);
160 * {@inheritDoc}
161 * @since 2017/12/31
163 @Override
164 public boolean isOptional()
166 return false;
170 * {@inheritDoc}
171 * @since 2017/12/31
173 @Override
174 public boolean matchesProvided(MarkedProvided __mp)
175 throws NullPointerException
177 if (__mp == null)
178 throw new NullPointerException("NARG");
180 return this.equals(__mp);
184 * {@inheritDoc}
185 * @since 2017/11/30
187 @Override
188 public String toString()
190 Reference<String> ref = this._string;
191 String rv;
193 if (ref == null || null == (rv = ref.get()))
194 this._string = new WeakReference<>((rv = "" +
195 this.name + "-" + this.version +
196 (this.compact ? "-compact" : "")));
198 return rv;
202 * Returns the version of this configuration.
204 * @return The configuration version.
205 * @since 2017/12/05
207 public SuiteVersion version()
209 return this.version;