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 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
;
16 * Defines a MIDlet that can be launched.
20 public final class JavaMEMidlet
22 /** Pseudo none class. */
23 public static final JavaMEMidlet NONE
=
24 new JavaMEMidlet("XSQUIRRELJMENONE", null,
27 /** Which MIDlet is to be run? */
28 public static final String MIDLET_PROPERTY
=
32 public final String title
;
35 public final String icon
;
37 /** The main class. */
38 public final String mainClass
;
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.
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.");
56 this.icon
= (__icon
== null ?
"" : __icon
);
57 this.mainClass
= __main
;
65 public final boolean equals(Object __o
)
70 if (!(__o
instanceof JavaMEMidlet
))
73 JavaMEMidlet o
= (JavaMEMidlet
)__o
;
74 return this.title
.equals(o
.title
) &&
75 this.icon
.equals(o
.icon
) &&
76 this.mainClass
.equals(o
.mainClass
);
84 public final int hashCode()
86 return this.title
.hashCode() ^
87 this.icon
.hashCode() ^
88 this.mainClass
.hashCode();
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.
110 public static JavaMEMidlet
find(List
<JavaMEMidlet
> __mids
)
111 throws NullPointerException
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;
122 number
= Integer
.parseInt(string
, 10);
124 catch (NumberFormatException ignored
)
128 // If this is a number...
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())
136 // If in range, return the exact MIDlet
137 if (number
>= 0 && number
<= __mids
.size())
138 return __mids
.get(number
);
141 // String based search
144 // Search for an exact matching case-insensitive title
145 for (JavaMEMidlet midlet
: __mids
)
146 if (string
.equalsIgnoreCase(midlet
.title
))
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
);
162 for (JavaMEMidlet midlet
: __mids
)
164 String title
= midlet
.title
.toLowerCase(Locale
.ROOT
);
166 // Does this match at all?
168 if (wildStart
&& wildEnd
)
169 matched
= title
.contains(sequence
);
171 matched
= title
.endsWith(sequence
);
173 matched
= title
.startsWith(sequence
);
175 // If matched, use it
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);