Remove Twitter links.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / JavaMEMidlet.java
blobac7c2ec4ecc6f36eafd8f0ecd97bd0bb4dcf9d2f
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.util.List;
13 import java.util.Locale;
15 /**
16 * Defines a MIDlet that can be launched.
18 * @since 2020/02/15
20 public final class JavaMEMidlet
22 /** Pseudo none class. */
23 public static final JavaMEMidlet NONE =
24 new JavaMEMidlet("XSQUIRRELJMENONE", null,
25 "XSQUIRRELJMENONE");
27 /** Which MIDlet is to be run? */
28 public static final String MIDLET_PROPERTY =
29 "squirreljme.midlet";
31 /** The title. */
32 public final String title;
34 /** The icon. */
35 public final String icon;
37 /** The main class. */
38 public final String mainClass;
40 /**
41 * Initializes the MIDlet information.
43 * @param __title The title of the MIDlet.
44 * @param __icon The icon of the MIDlet, may be {@code null}.
45 * @param __main The main class.
46 * @throws NullPointerException If no title or main class was specified.
47 * @since 2020/02/15
49 public JavaMEMidlet(String __title, String __icon, String __main)
50 throws NullPointerException
52 if (__title == null || __main == null)
53 throw new NullPointerException("No title or main specified.");
55 this.title = __title;
56 this.icon = (__icon == null ? "" : __icon);
57 this.mainClass = __main;
60 /**
61 * {@inheritDoc}
62 * @since 2020/02/15
64 @Override
65 public final boolean equals(Object __o)
67 if (this == __o)
68 return true;
70 if (!(__o instanceof JavaMEMidlet))
71 return false;
73 JavaMEMidlet o = (JavaMEMidlet)__o;
74 return this.title.equals(o.title) &&
75 this.icon.equals(o.icon) &&
76 this.mainClass.equals(o.mainClass);
79 /**
80 * {@inheritDoc}
81 * @since 2020/02/15
83 @Override
84 public final int hashCode()
86 return this.title.hashCode() ^
87 this.icon.hashCode() ^
88 this.mainClass.hashCode();
91 /**
92 * {@inheritDoc}
93 * @since 2020/02/15
95 @Override
96 public final String toString()
98 return String.format("%s, %s, %s",
99 this.title, this.icon, this.mainClass);
103 * Search through the list of MIDlets and also
105 * @param __mids The MIDlets to search within.
106 * @return The MIDlet to be used, may be {@code null}.
107 * @throws NullPointerException On null arguments.
108 * @since 2020/07/25
110 public static JavaMEMidlet find(List<JavaMEMidlet> __mids)
111 throws NullPointerException
113 if (__mids == null)
114 throw new NullPointerException("NARG");
116 // Decode the values it could possibly be
117 String string = System.getProperty(JavaMEMidlet.MIDLET_PROPERTY);
118 Integer number = null;
119 if (string != null)
122 number = Integer.parseInt(string, 10);
124 catch (NumberFormatException ignored)
128 // If this is a number...
129 if (number != null)
131 // Force the selection of no MIDlet (run main class)
132 // Even so if there are no MIDlets available
133 if (number == -1 || __mids.isEmpty())
134 return null;
136 // If in range, return the exact MIDlet
137 if (number >= 0 && number <= __mids.size())
138 return __mids.get(number);
141 // String based search
142 if (string != null)
144 // Search for an exact matching case-insensitive title
145 for (JavaMEMidlet midlet : __mids)
146 if (string.equalsIgnoreCase(midlet.title))
147 return midlet;
149 // Wildcard name matching
150 boolean wildStart = string.startsWith("*");
151 boolean wildEnd = string.endsWith("*");
152 if (wildStart || wildEnd)
154 // Remove the asterisks
155 String sequence = (wildStart ? (wildEnd ?
156 string.substring(1, string.length() - 1) :
157 string.substring(1)) :
158 string.substring(0, string.length() - 1))
159 .toLowerCase(Locale.ROOT);
161 // Scan MIDlets
162 for (JavaMEMidlet midlet : __mids)
164 String title = midlet.title.toLowerCase(Locale.ROOT);
166 // Does this match at all?
167 boolean matched;
168 if (wildStart && wildEnd)
169 matched = title.contains(sequence);
170 else if (wildStart)
171 matched = title.endsWith(sequence);
172 else
173 matched = title.startsWith(sequence);
175 // If matched, use it
176 if (matched)
177 return midlet;
181 // Otherwise, construct a new MIDlet entry point
182 return new JavaMEMidlet(string, null, string);
185 // Use the first MIDlet if available, or otherwise stop
186 if (!__mids.isEmpty())
187 return __mids.get(0);
188 return null;