Cherry pick String off-loading improvements from `wip-nanocoatexec`.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / JavaMEProfile.java
blobfb76e9326c025c7fe19f79491b2fe6b5e27d6904
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 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;
17 import java.util.Set;
19 /**
20 * This represents a profile that may be implemented, such as MIDP.
22 * @since 2016/12/14
24 public final class JavaMEProfile
25 implements Comparable<JavaMEProfile>, MarkedDependency, MarkedProvided
27 /** Name. */
28 protected final APIName name;
30 /** Version. */
31 protected final SuiteVersion version;
33 /** String representation. */
34 private Reference<String> _string;
36 /**
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.
42 * @since 2017/11/30
44 public JavaMEProfile(APIName __n, SuiteVersion __v)
45 throws NullPointerException
47 if (__n == null)
48 throw new NullPointerException("NARG");
50 this.name = __n;
51 this.version = __v;
54 /**
55 * Initializes the profile by parsing the given string.
57 * @param __n The string to parse.
58 * @throws NullPointerException On null arguments.
59 * @since 2017/11/30
61 public JavaMEProfile(String __n)
62 throws NullPointerException
64 if (__n == null)
65 throw new NullPointerException("NARG");
67 // No version specified
68 int n = __n.length(),
69 dx = __n.lastIndexOf('-');
70 char c;
71 if (dx < 0 || dx + 1 >= n || (c = __n.charAt(dx + 1)) < '0' || c > '9')
73 this.name = new APIName(__n);
74 this.version = null;
77 // There is a version
78 else
80 this.name = new APIName(__n.substring(0, dx));
81 this.version = new SuiteVersion(__n.substring(dx + 1));
85 /**
86 * {@inheritDoc}
87 * @since 2017/11/30
89 @Override
90 public int compareTo(JavaMEProfile __o)
92 int rv = this.name.compareTo(__o.name);
93 if (rv != 0)
94 return rv;
96 SuiteVersion a = this.version,
97 b = __o.version;
98 if ((a == null) != (b == null))
99 return (a == null ? -1 : 1);
100 else if (a != null)
101 return a.compareTo(b);
102 return 0;
106 * {@inheritDoc}
107 * @since 2017/11/30
109 @Override
110 public boolean equals(Object __o)
112 if (this == __o)
113 return true;
115 if (!(__o instanceof JavaMEProfile))
116 return false;
118 JavaMEProfile o = (JavaMEProfile)__o;
119 return this.name.equals(o.name) &&
120 Objects.equals(this.version, o.version);
124 * {@inheritDoc}
125 * @since 2017/11/30
127 @Override
128 public int hashCode()
130 return this.name.hashCode() ^
131 Objects.hashCode(this.version);
135 * {@inheritDoc}
136 * @since 2017/12/31
138 @Override
139 public boolean isOptional()
141 return false;
145 * {@inheritDoc}
146 * @since 2017/12/31
148 @Override
149 public boolean matchesProvided(MarkedProvided __mp)
150 throws NullPointerException
152 if (__mp == null)
153 throw new NullPointerException("NARG");
155 return this.equals(__mp);
159 * {@inheritDoc}
160 * @since 2017/11/30
162 @Override
163 public String toString()
165 Reference<String> ref = this._string;
166 String rv;
168 if (ref == null || null == (rv = ref.get()))
169 this._string = new WeakReference<>(
170 (rv = this.name + "-" + this.version));
172 return rv;
176 * Returns the version of this profile.
178 * @return The profile version.
179 * @since 2017/12/05
181 public SuiteVersion version()
183 return this.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.
192 * @since 2022/08/09
194 public static Set<JavaMEProfile> parseProfiles(String __input)
195 throws NullPointerException
197 if (__input == null)
198 throw new NullPointerException("NARG");
200 Set<JavaMEProfile> result = new LinkedHashSet<>();
201 for (String value : __input.split(" \t"))
202 result.add(new JavaMEProfile(value));
204 return result;
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.
213 * @since 2022/08/09
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)
224 if (sb.length() > 0)
225 sb.append(' ');
227 sb.append(profile);
230 return sb.toString();