Indentations break the feed.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / suite / Profile.java
blobed7e19fa24189131e054d4bf4fe13f296107eabe
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.jvm.suite;
12 import java.lang.ref.Reference;
13 import java.lang.ref.WeakReference;
14 import java.util.Objects;
16 /**
17 * This represents a profile that may be implemented, such as MIDP.
19 * @since 2016/12/14
21 public final class Profile
22 implements Comparable<Profile>, MarkedDependency, MarkedProvided
24 /** Name. */
25 protected final APIName name;
27 /** Version. */
28 protected final SuiteVersion version;
30 /** String representation. */
31 private Reference<String> _string;
33 /**
34 * Initializes the profile using the given API name and version.
36 * @param __n The name to use.
37 * @param __v The version of the suite, this is optional.
38 * @throws NullPointerException If no name was specified.
39 * @since 2017/11/30
41 public Profile(APIName __n, SuiteVersion __v)
42 throws NullPointerException
44 if (__n == null)
45 throw new NullPointerException("NARG");
47 this.name = __n;
48 this.version = __v;
51 /**
52 * Initializes the profile by parsing the given string.
54 * @param __n The string to parse.
55 * @throws NullPointerException On null arguments.
56 * @since 2017/11/30
58 public Profile(String __n)
59 throws NullPointerException
61 if (__n == null)
62 throw new NullPointerException("NARG");
64 // No version specified
65 int n = __n.length(),
66 dx = __n.lastIndexOf('-');
67 char c;
68 if (dx < 0 || dx + 1 >= n || (c = __n.charAt(dx + 1)) < '0' || c > '9')
70 this.name = new APIName(__n);
71 this.version = null;
74 // There is a version
75 else
77 this.name = new APIName(__n.substring(0, dx));
78 this.version = new SuiteVersion(__n.substring(dx + 1));
82 /**
83 * Returns the API name.
85 * @return The API name.
86 * @since 2022/02/28
88 public APIName apiName()
90 return this.name;
93 /**
94 * {@inheritDoc}
95 * @since 2017/11/30
97 @Override
98 public int compareTo(Profile __o)
100 int rv = this.name.compareTo(__o.name);
101 if (rv != 0)
102 return rv;
104 SuiteVersion a = this.version,
105 b = __o.version;
106 if ((a == null) != (b == null))
107 return (a == null ? -1 : 1);
108 else if (a != null)
109 return a.compareTo(b);
110 return 0;
114 * {@inheritDoc}
115 * @since 2017/11/30
117 @Override
118 public boolean equals(Object __o)
120 if (this == __o)
121 return true;
123 if (!(__o instanceof Profile))
124 return false;
126 Profile o = (Profile)__o;
127 return this.name.equals(o.name) &&
128 Objects.equals(this.version, o.version);
132 * {@inheritDoc}
133 * @since 2017/11/30
135 @Override
136 public int hashCode()
138 return this.name.hashCode() ^
139 Objects.hashCode(this.version);
143 * {@inheritDoc}
144 * @since 2017/12/31
146 @Override
147 public boolean isOptional()
149 return false;
153 * {@inheritDoc}
154 * @since 2017/12/31
156 @Override
157 public boolean matchesProvided(MarkedProvided __mp)
158 throws NullPointerException
160 if (__mp == null)
161 throw new NullPointerException("NARG");
163 return this.equals(__mp);
167 * {@inheritDoc}
168 * @since 2017/11/30
170 @Override
171 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 = "Profile " +
179 this.name + ":" + this.version));
181 return rv;
185 * Returns the version of this profile.
187 * @return The profile version.
188 * @since 2017/12/05
190 public SuiteVersion version()
192 return this.version;