RFR(m): 8139233u1 add initial compact immutable collection implementations
Remi Forax
forax at univ-mlv.fr
Mon May 9 14:25:23 UTC 2016
Hi Stuart,
I still think that the code of hasNext of the iterator of SetN and MapN should not do any side effect.
otherwise,
in the constructor of MapN, you can declare k and v as Object
(the table is an array of Object and probe() now takes an Object)
also instead of InternalError, i think AssertionError is a better exception,
the javadoc of InternalError says that InternalError is thrown by the VM.
in MapN.probe, you can remove the cast to K (and the corresponding @SuppressWarnings).
Set2.e0 and Set2.e1 should be declared as package-private otherwise, the compiler generates an accessor in the iterator. Same thing for SetN.elements, MapN.table and MapN.size
CollSer implementation can be improved to avoid the ugly switch/case by replacing the constant list by an enum,
something like the code below. In that case, creating a CollSer for a Map will be done like this,
return new CollSer(CollSer.Kind.MAP, array);
final class CollSer implements Serializable {
private static final long serialVersionUID = 6309168927139932177L;
//static final int IMM_LIST = 1;
//static final int IMM_SET = 2;
//static final int IMM_MAP = 3;
enum Kind {
LIST(List::of),
SET(Set::of),
MAP(Kind::mapFromArray);
final Function<Object[], Object> factory;
private Kind(Function<Object[], Object> factory) {
this.factory = factory;
}
private static Map<?,?> mapFromArray(Object[] array) {
if (array.length == 0) {
return new ImmutableCollections.Map0<>();
}
if (array.length == 2) {
return new ImmutableCollections.Map1<>(array[0], array[1]);
}
return new ImmutableCollections.MapN<>(array);
}
}
private static final Kind[] KINDS = Kind.values();
private final int kind;
private final Object[] array;
CollSer(Kind kind, Object... array) {
this.kind = kind.ordinal();
this.array = array;
}
private Object readResolve() throws ObjectStreamException {
if (array == null) {
throw new InvalidObjectException("null array");
}
try {
return KINDS[kind].factory.apply(array);
} catch (NullPointerException|IllegalArgumentException|ArrayIndexOutOfBoundsException ex) {
InvalidObjectException ioe = new InvalidObjectException("invalid object");
ioe.initCause(ex);
throw ioe;
}
}
}
Rémi
----- Mail original -----
> De: "Stuart Marks" <stuart.marks at oracle.com>
> À: "Michael Hixson" <michael.hixson at gmail.com>
> Cc: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Vendredi 6 Mai 2016 20:04:27
> Objet: Re: RFR(m): 8139233u1 add initial compact immutable collection implementations
>
> On 5/6/16 10:57 AM, Stuart Marks wrote:
> > I've been doing too much last-minute refactoring.
>
> Actually that's wasn't the cause of this bug. The bug was in the previous
> webrev
> and also in the prototype from my branch in the sandbox, so it's been in
> there
> quite a while.
>
> Again, thanks for spotting it.
>
> s'marks
>
More information about the core-libs-dev
mailing list