Closed GoogleCodeExporter closed 9 years ago
I have identified the problem. TreeMap includes a no-arg constructor that
leaves the comparator null. This allows you to use keys that implement
Comparable already. In most of Java, this includes things like Number types
and Strings. The full list for JDK 6 is:
All Known Implementing Classes:
Authenticator.RequestorType, BigDecimal, BigInteger, Boolean, Byte, ByteBuffer,
Calendar, Character, CharBuffer, Charset, ClientInfoStatus, CollationKey,
Component.BaselineResizeBehavior, CompositeName, CompoundName, Date, Date,
Desktop.Action, Diagnostic.Kind, Dialog.ModalExclusionType,
Dialog.ModalityType, Double, DoubleBuffer, DropMode, ElementKind, ElementType,
Enum, File, Float, FloatBuffer, Formatter.BigDecimalLayoutForm,
FormSubmitEvent.MethodType, GregorianCalendar, GroupLayout.Alignment,
IntBuffer, Integer, JavaFileObject.Kind, JTable.PrintMode, KeyRep.Type,
LayoutStyle.ComponentPlacement, LdapName, Long, LongBuffer, MappedByteBuffer,
MemoryType, MessageContext.Scope, Modifier,
MultipleGradientPaint.ColorSpaceType, MultipleGradientPaint.CycleMethod,
NestingKind, Normalizer.Form, ObjectName, ObjectStreamField, Proxy.Type, Rdn,
Resource.AuthenticationType, RetentionPolicy, RoundingMode,
RowFilter.ComparisonType, RowIdLifetime, RowSorterEvent.Type, Service.Mode,
Short, ShortBuffer, SOAPBinding.ParameterStyle, SOAPBinding.Style,
SOAPBinding.Use, SortOrder, SourceVersion, SSLEngineResult.HandshakeStatus,
SSLEngineResult.Status, StandardLocation, String, SwingWorker.StateValue,
Thread.State, Time, Timestamp, TimeUnit, TrayIcon.MessageType, TypeKind, URI,
UUID, WebParam.Mode, XmlAccessOrder, XmlAccessType, XmlNsForm
Unfortunately in our API the only classes that implement Comparable are:
All Known Implementing Classes:
ElementType, Enum, RetentionPolicy
There are 3 possible solutions:
1. Throw an exception when a no-arg constructor is called for TreeMap (and
likely any form of sorted set, map, or list because they all work the same way.
2. Change the number classes to implement Comparable. This may be difficult
to maintain consistency on J2ME.
3. Add special handling to the no-arg constructors to create appropriate
comparables for Number types.
My preferred option is #3. My proposed approach is to implement package
private Comparable implementations for all of the boxed number types, then add
special handling to the no-arg constructors of
TreeSet and TreeMap to instantiate the appropriate comparable when the key type
is a boxed numeric type.
Do you have any objections to this approach?
If I went this route, I assume I would need to make the change in both the
CLDC11 project and the JavaAPI project. Is that correct?
Original comment by steve.ha...@codenameone.com
on 27 Jan 2015 at 5:37
I went ahead and fixed this with option 3.
https://code.google.com/p/codenameone/source/detail?r=2043
I also patched the CLDC11 project with the same changes to make compatible with
J2ME/BB.
Original comment by st...@weblite.ca
on 27 Jan 2015 at 6:07
Changing the CLDC11 project isn't the right thing to do here. Since there is no
API change your changes are meaningless as the CLDC11 project is mostly just
stubs.
Original comment by shai.almog
on 27 Jan 2015 at 6:08
How does J2ME make use of the java collections classes? Do they have their own
copy or do they get them from the CLDC11 jar?
Original comment by st...@weblite.ca
on 27 Jan 2015 at 6:16
There is code that is executed from the builder. Its in the JavaCompatibility
project. It has a rather different structure since it can't occupy java.*.
Original comment by shai.almog
on 27 Jan 2015 at 7:46
OK I have reversed the change to CLDC11. Will look into the JavaCompatibility
project.
Original comment by st...@weblite.ca
on 27 Jan 2015 at 7:57
Original issue reported on code.google.com by
st...@weblite.ca
on 24 Jan 2015 at 6:04Attachments: