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 java
.lang
.ref
.Reference
;
13 import java
.lang
.ref
.WeakReference
;
14 import java
.util
.Collection
;
15 import java
.util
.LinkedHashSet
;
16 import java
.util
.Objects
;
20 * This represents a profile that may be implemented, such as MIDP.
24 public final class JavaMEProfile
25 implements Comparable
<JavaMEProfile
>, MarkedDependency
, MarkedProvided
28 protected final APIName name
;
31 protected final SuiteVersion version
;
33 /** String representation. */
34 private Reference
<String
> _string
;
37 * Initializes the profile using the given API name and version.
39 * @param __n The name to use.
40 * @param __v The version of the suite, this is optional.
41 * @throws NullPointerException If no name was specified.
44 public JavaMEProfile(APIName __n
, SuiteVersion __v
)
45 throws NullPointerException
48 throw new NullPointerException("NARG");
55 * Initializes the profile by parsing the given string.
57 * @param __n The string to parse.
58 * @throws NullPointerException On null arguments.
61 public JavaMEProfile(String __n
)
62 throws NullPointerException
65 throw new NullPointerException("NARG");
67 // No version specified
69 dx
= __n
.lastIndexOf('-');
71 if (dx
< 0 || dx
+ 1 >= n
|| (c
= __n
.charAt(dx
+ 1)) < '0' || c
> '9')
73 this.name
= new APIName(__n
);
80 this.name
= new APIName(__n
.substring(0, dx
));
81 this.version
= new SuiteVersion(__n
.substring(dx
+ 1));
90 public int compareTo(JavaMEProfile __o
)
92 int rv
= this.name
.compareTo(__o
.name
);
96 SuiteVersion a
= this.version
,
98 if ((a
== null) != (b
== null))
99 return (a
== null ?
-1 : 1);
101 return a
.compareTo(b
);
110 public boolean equals(Object __o
)
115 if (!(__o
instanceof JavaMEProfile
))
118 JavaMEProfile o
= (JavaMEProfile
)__o
;
119 return this.name
.equals(o
.name
) &&
120 Objects
.equals(this.version
, o
.version
);
128 public int hashCode()
130 return this.name
.hashCode() ^
131 Objects
.hashCode(this.version
);
139 public boolean isOptional()
149 public boolean matchesProvided(MarkedProvided __mp
)
150 throws NullPointerException
153 throw new NullPointerException("NARG");
155 return this.equals(__mp
);
163 public String
toString()
165 Reference
<String
> ref
= this._string
;
168 if (ref
== null || null == (rv
= ref
.get()))
169 this._string
= new WeakReference
<>(
170 (rv
= this.name
+ "-" + this.version
));
176 * Returns the version of this profile.
178 * @return The profile version.
181 public SuiteVersion
version()
187 * Parses a list of profiles from the string.
189 * @param __input The string to parse from.
190 * @return The parsed profiles.
191 * @throws NullPointerException On null arguments.
194 public static Set
<JavaMEProfile
> parseProfiles(String __input
)
195 throws NullPointerException
198 throw new NullPointerException("NARG");
200 Set
<JavaMEProfile
> result
= new LinkedHashSet
<>();
201 for (String value
: __input
.split(" \t"))
202 result
.add(new JavaMEProfile(value
));
208 * Converts the collection of profiles to a string used in property files.
210 * @param __profiles The profile to convert.
211 * @return The converted property string.
212 * @throws NullPointerException On null arguments.
215 public static String
toString(Collection
<JavaMEProfile
> __profiles
)
216 throws NullPointerException
218 if (__profiles
== null)
219 throw new NullPointerException("NARG");
221 StringBuilder sb
= new StringBuilder();
222 for (JavaMEProfile profile
: __profiles
)
230 return sb
.toString();