Make it so mapping files are used and then reapplied.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / suite / Configuration.java
blob71948603190e2edf7bc69ddbff00b711278d8851
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 GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jvm.suite;
12 import cc.squirreljme.runtime.cldc.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 Configuration
25 implements Comparable<Configuration>, MarkedDependency, MarkedProvided
27 /** Name. */
28 protected final APIName name;
30 /** Version. */
31 protected final SuiteVersion version;
33 /** Is this configuration compact? */
34 protected final boolean compact;
36 /** String representation. */
37 private Reference<String> _string;
39 /**
40 * Initializes the configuration using the given API name and version.
42 * @param __n The name to use.
43 * @param __v The version of the suite.
44 * @param __c If {@code true} then the configuration is compact.
45 * @throws NullPointerException On null arguments.
46 * @since 2017/11/30
48 public Configuration(APIName __n, SuiteVersion __v, boolean __c)
49 throws NullPointerException
51 if (__n == null || __v == null)
52 throw new NullPointerException("NARG");
54 // Set
55 this.name = __n;
56 this.version = __v;
57 this.compact = __c;
60 /**
61 * Initializes the configuration by parsing the given string.
63 * @param __n The string to parse.
64 * @throws NullPointerException On null arguments.
65 * @since 2017/11/30
67 public Configuration(String __n)
68 throws NullPointerException
70 if (__n == null)
71 throw new NullPointerException("NARG");
73 // {@squirreljme.error DG02 Expected two or three fields for the
74 // configuration. (The input string)}
75 String[] fields = StringUtils.fieldSplit('-', __n);
76 int fn = fields.length;
77 if (fn != 2 && fn != 3)
78 throw new InvalidSuiteException(String.format("AR02 %s", __n));
80 // Potentially compact?
81 this.compact = (fn > 2 &&
82 0 == fields[2].compareToIgnoreCase("compact"));
84 // Parse name and version
85 this.name = new APIName(fields[0]);
86 this.version = new SuiteVersion(fields[1]);
89 /**
90 * {@inheritDoc}
91 * @since 2017/11/30
93 @Override
94 public int compareTo(Configuration __o)
96 int rv = this.name.compareTo(__o.name);
97 if (rv != 0)
98 return rv;
100 rv = this.version.compareTo(__o.version);
101 if (rv != 0)
102 return rv;
104 // Compact is before non-compact
105 boolean a = this.compact,
106 b = __o.compact;
107 if (a != b)
108 return (a ? -1 : 1);
109 return 0;
113 * {@inheritDoc}
114 * @since 2017/11/30
116 @Override
117 public boolean equals(Object __o)
119 if (this == __o)
120 return true;
122 if (!(__o instanceof Configuration))
123 return false;
125 Configuration o = (Configuration)__o;
126 return this.name.equals(o.name) &&
127 this.version.equals(o.version) &&
128 this.compact == o.compact;
132 * {@inheritDoc}
133 * @since 2017/11/30
135 @Override
136 public int hashCode()
138 return this.name.hashCode() ^
139 Objects.hashCode(this.version) ^
140 (this.compact ? 0xFFFFFFFF : 0);
144 * {@inheritDoc}
145 * @since 2017/12/31
147 @Override
148 public boolean isOptional()
150 return false;
154 * {@inheritDoc}
155 * @since 2017/12/31
157 @Override
158 public boolean matchesProvided(MarkedProvided __mp)
159 throws NullPointerException
161 if (__mp == null)
162 throw new NullPointerException("NARG");
164 return this.equals(__mp);
168 * {@inheritDoc}
169 * @since 2017/11/30
171 @Override
172 public String toString()
174 Reference<String> ref = this._string;
175 String rv;
177 if (ref == null || null == (rv = ref.get()))
178 this._string = new WeakReference<>((rv = "Configuration " +
179 this.name + ":" + this.version +
180 (this.compact ? "-compact" : "")));
182 return rv;
186 * Returns the version of this configuration.
188 * @return The configuration version.
189 * @since 2017/12/05
191 public SuiteVersion version()
193 return this.version;