1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
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.
24 public final class JavaMEConfiguration
25 implements Comparable
<JavaMEConfiguration
>, MarkedDependency
, MarkedProvided
27 /** CDC Application. */
28 public static final APIName CDC_NAME
=
31 /** CLDC Application. */
32 public static final APIName CLDC_NAME
=
36 protected final APIName name
;
39 protected final SuiteVersion version
;
41 /** Is this configuration compact? */
42 protected final boolean compact
;
44 /** String representation. */
45 private Reference
<String
> _string
;
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.
56 public JavaMEConfiguration(APIName __n
, SuiteVersion __v
, boolean __c
)
57 throws NullPointerException
59 if (__n
== null || __v
== null)
60 throw new NullPointerException("NARG");
69 * Initializes the configuration by parsing the given string.
71 * @param __n The string to parse.
72 * @throws NullPointerException On null arguments.
75 public JavaMEConfiguration(String __n
)
76 throws NullPointerException
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]);
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
))
112 int rv
= aName
.compareTo(bName
);
116 rv
= this.version
.compareTo(__o
.version
);
120 // Compact is before non-compact
121 boolean a
= this.compact
,
133 public boolean equals(Object __o
)
138 if (!(__o
instanceof JavaMEConfiguration
))
141 JavaMEConfiguration o
= (JavaMEConfiguration
)__o
;
142 return this.name
.equals(o
.name
) &&
143 this.version
.equals(o
.version
) &&
144 this.compact
== o
.compact
;
152 public int hashCode()
154 return this.name
.hashCode() ^
155 Objects
.hashCode(this.version
) ^
156 (this.compact ?
0xFFFFFFFF : 0);
164 public boolean isOptional()
174 public boolean matchesProvided(MarkedProvided __mp
)
175 throws NullPointerException
178 throw new NullPointerException("NARG");
180 return this.equals(__mp
);
188 public String
toString()
190 Reference
<String
> ref
= this._string
;
193 if (ref
== null || null == (rv
= ref
.get()))
194 this._string
= new WeakReference
<>((rv
= "" +
195 this.name
+ "-" + this.version
+
196 (this.compact ?
"-compact" : "")));
202 * Returns the version of this configuration.
204 * @return The configuration version.
207 public SuiteVersion
version()