Cherry pick the banglets and such from wip-l1summercoat, this will be the basis for...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / JavaMEMidlet.java
blobe4b7a03e104a4d66a8db2061e6893e6886356e1b
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 GNU General Public License v3+, or later.
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 /** Which MIDlet is to be run? */
23 public static final String MIDLET_PROPERTY =
24 "squirreljme.midlet";
26 /** The title. */
27 public final String title;
29 /** The icon. */
30 public final String icon;
32 /** The main class. */
33 public final String mainClass;
35 /**
36 * Initializes the MIDlet information.
38 * @param __title The title of the MIDlet.
39 * @param __icon The icon of the MIDlet, may be {@code null}.
40 * @param __main The main class.
41 * @throws NullPointerException If no title or main class was specified.
42 * @since 2020/02/15
44 public JavaMEMidlet(String __title, String __icon, String __main)
45 throws NullPointerException
47 if (__title == null || __main == null)
48 throw new NullPointerException("No title or main specified.");
50 this.title = __title;
51 this.icon = (__icon == null ? "" : __icon);
52 this.mainClass = __main;
55 /**
56 * {@inheritDoc}
57 * @since 2020/02/15
59 @Override
60 public final boolean equals(Object __o)
62 if (this == __o)
63 return true;
65 if (!(__o instanceof JavaMEMidlet))
66 return false;
68 JavaMEMidlet o = (JavaMEMidlet)__o;
69 return this.title.equals(o.title) &&
70 this.icon.equals(o.icon) &&
71 this.mainClass.equals(o.mainClass);
74 /**
75 * {@inheritDoc}
76 * @since 2020/02/15
78 @Override
79 public final int hashCode()
81 return this.title.hashCode() ^
82 this.icon.hashCode() ^
83 this.mainClass.hashCode();
86 /**
87 * {@inheritDoc}
88 * @since 2020/02/15
90 @Override
91 public final String toString()
93 return String.format("%s, %s, %s",
94 this.title, this.icon, this.mainClass);
97 /**
98 * Search through the list of MIDlets and also
100 * @param __mids The MIDlets to search within.
101 * @return The MIDlet to be used, may be {@code null}.
102 * @throws NullPointerException On null arguments.
103 * @since 2020/07/25
105 public static JavaMEMidlet find(List<JavaMEMidlet> __mids)
106 throws NullPointerException
108 if (__mids == null)
109 throw new NullPointerException("NARG");
111 // Decode the values it could possibly be
112 String string = System.getProperty(JavaMEMidlet.MIDLET_PROPERTY);
113 Integer number = null;
114 if (string != null)
117 number = Integer.parseInt(string, 10);
119 catch (NumberFormatException ignored)
123 // If this is a number...
124 if (number != null)
126 // Force the selection of no MIDlet (run main class)
127 // Even so if there are no MIDlets available
128 if (number == -1 || __mids.isEmpty())
129 return null;
131 // If in range, return the exact MIDlet
132 if (number >= 0 && number <= __mids.size())
133 return __mids.get(number);
136 // String based search
137 if (string != null)
139 // Search for an exact matching case-insensitive title
140 for (JavaMEMidlet midlet : __mids)
141 if (string.equalsIgnoreCase(midlet.title))
142 return midlet;
144 // Wildcard name matching
145 boolean wildStart = string.startsWith("*");
146 boolean wildEnd = string.endsWith("*");
147 if (wildStart || wildEnd)
149 // Remove the asterisks
150 String sequence = (wildStart ? (wildEnd ?
151 string.substring(1, string.length() - 1) :
152 string.substring(1)) :
153 string.substring(0, string.length() - 1))
154 .toLowerCase(Locale.ROOT);
156 // Scan MIDlets
157 for (JavaMEMidlet midlet : __mids)
159 String title = midlet.title.toLowerCase(Locale.ROOT);
161 // Does this match at all?
162 boolean matched;
163 if (wildStart && wildEnd)
164 matched = title.contains(sequence);
165 else if (wildStart)
166 matched = title.endsWith(sequence);
167 else
168 matched = title.startsWith(sequence);
170 // If matched, use it
171 if (matched)
172 return midlet;
176 // Otherwise, construct a new MIDlet entry point
177 return new JavaMEMidlet(string, null, string);
180 // Use the first MIDlet if available, or otherwise stop
181 if (!__mids.isEmpty())
182 return __mids.get(0);
183 return null;