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 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
;
16 * Defines a MIDlet that can be launched.
20 public final class JavaMEMidlet
22 /** Which MIDlet is to be run? */
23 public static final String MIDLET_PROPERTY
=
27 public final String title
;
30 public final String icon
;
32 /** The main class. */
33 public final String mainClass
;
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.
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.");
51 this.icon
= (__icon
== null ?
"" : __icon
);
52 this.mainClass
= __main
;
60 public final boolean equals(Object __o
)
65 if (!(__o
instanceof JavaMEMidlet
))
68 JavaMEMidlet o
= (JavaMEMidlet
)__o
;
69 return this.title
.equals(o
.title
) &&
70 this.icon
.equals(o
.icon
) &&
71 this.mainClass
.equals(o
.mainClass
);
79 public final int hashCode()
81 return this.title
.hashCode() ^
82 this.icon
.hashCode() ^
83 this.mainClass
.hashCode();
91 public final String
toString()
93 return String
.format("%s, %s, %s",
94 this.title
, this.icon
, this.mainClass
);
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.
105 public static JavaMEMidlet
find(List
<JavaMEMidlet
> __mids
)
106 throws NullPointerException
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;
117 number
= Integer
.parseInt(string
, 10);
119 catch (NumberFormatException ignored
)
123 // If this is a number...
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())
131 // If in range, return the exact MIDlet
132 if (number
>= 0 && number
<= __mids
.size())
133 return __mids
.get(number
);
136 // String based search
139 // Search for an exact matching case-insensitive title
140 for (JavaMEMidlet midlet
: __mids
)
141 if (string
.equalsIgnoreCase(midlet
.title
))
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
);
157 for (JavaMEMidlet midlet
: __mids
)
159 String title
= midlet
.title
.toLowerCase(Locale
.ROOT
);
161 // Does this match at all?
163 if (wildStart
&& wildEnd
)
164 matched
= title
.contains(sequence
);
166 matched
= title
.endsWith(sequence
);
168 matched
= title
.startsWith(sequence
);
170 // If matched, use it
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);