This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 73637 - NbCollections & Union2
Summary: NbCollections & Union2
Alias: None
Product: platform
Classification: Unclassified
Component: -- Other -- (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: apireviews
Depends on:
Reported: 2006-03-16 10:35 UTC by Jesse Glick
Modified: 2008-12-22 19:23 UTC (History)
1 user (show)

See Also:
Exception Reporter:

Proposed patch (117.01 KB, patch)
2006-06-04 17:20 UTC, Jesse Glick
Details | Diff
Some further uses of Union2 in core/bootstrap (27.53 KB, patch)
2006-06-04 18:03 UTC, Jesse Glick
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jesse Glick 2006-03-16 10:35:06 UTC
I just made a class Generics in the Ant module that includes

/** copies a map and checks casts as it copies */
public static <K,V> Map<K,V> checkedMapByCopy(Map m, Class<K> k, Class<V> v)
throws ClassCastException;

/** returns proxy set backed by a raw set; iterator etc. quietly remove any
elements not of desired type */
public static <E> Set<E> checkedSetByFilter(Set s, Class<E> e);

/** union class; contains either first type or second type */
public static abstract class Union2<First,Second> {
    public abstract First first() throws IllegalArgumentException;
    public abstract Second second() throws IllegalArgumentException;
    public abstract boolean hasFirst();
    public abstract boolean hasSecond();
public static <First,Second> Union2<First,Second> union2First(First first);
public static <First,Second> Union2<First,Second> union2Second(Second second);

Unit tests also in ant module.

Can these things go into openide/util somewhere?

Also would like to add to e.g. Enumerations:

public static <E> Enumeration<E> checkedEnumerationByFilter(Enumeration e,
Class<E> c);

which would quietly remove any objects not null or assignable to E.

And perhaps other related things, e.g. Union3 (if ever needed),
checkedSetByCopy, checkedMapByFilter, etc. The current stuff is just what I
needed in the ant module.
Comment 1 Jesse Glick 2006-03-24 14:18:42 UTC
Also could be used in NbBundle.
Comment 2 Jesse Glick 2006-03-30 17:32:48 UTC
Union2 could be used in o.n.JarClassLoader, o.n.Main, o.n.ModuleManager, etc. -
just grep for '<Object'.
Comment 3 _ rkubacki 2006-03-30 20:16:58 UTC
It can. If someone thinks it is important file a task for me once the API is
there and I can convert these classes.
Comment 4 Jesse Glick 2006-04-09 18:50:22 UTC
I have made some refinements and additions. There is now

[no checkedListBy* for now; haven't seen a need, but could be created later]

All are fully documented in the Ant module, and unit test coverage is maximal
acc. to Emma:

Would appreciate quick review, comments. Else I plan to commit in a few days
time; would probably put checkedEnumerationByFilter into Enumerations, and all
else into o.o.u.Generics, unless someone has a better idea.
Comment 5 Jaroslav Tulach 2006-04-10 06:13:33 UTC
Maybe NbCollections would be better name(p3)?
Maybe it would be better to start NetBeans Utilities module and put the class 
to org.nb.api.util? That way it need not be on classpath and we would have a 
place to add util stuff to without growing the size of "NetBeans Runtime 
Comment 6 Jesse Glick 2006-04-10 17:19:49 UTC
Re. NbCollections - perhaps. Odd for Union2 however.

An idea: would people prefer to see Generics.Union2 (with the static factory
methods in Generics) or a top-level class Union2 (with the factory methods
inside it)? If the latter, then of course NbCollections for the remaining
methods would make more sense, since they are very much analogous to methods in
j.u.Collections. (The existing j.u.C.checked* methods are not so helpful because
they assume the incoming collection is already well-typed, which is exactly what
the new methods do not assume.)

Re. new util module - possible but

1. It means we cannot use the new methods in some places like NbBundle or
JarClassLoader where they would be helpful (not required).

2. If we have openide/util with miscellaneous utility classes, and another
(true) module with miscellaneous utility classes, how does someone know where to
look for a utility class? Seems a bit arbitrary. Would be nicer to split off
logical subportions of openide/util into new modules, e.g. for action
infrastructure or data transfer or threading or lookup, and keep openide/util
for truly general-purpose utilities.

But I don't know.
Comment 7 Jesse Glick 2006-06-04 17:20:32 UTC
Created attachment 30774 [details]
Proposed patch
Comment 8 Jesse Glick 2006-06-04 17:23:41 UTC
I've done some more cleanup and employed the new APIs in several places. See
attached patch, from branch nbcollections_73637. I plan to merge this early next
week if there are no objections.
Comment 9 Jesse Glick 2006-06-04 18:03:08 UTC
Created attachment 30775 [details]
Some further uses of Union2 in core/bootstrap
Comment 10 Jaroslav Tulach 2006-06-05 07:51:42 UTC
I do not like much Union2.union2first(First f) it repeats the same information 
twice. I guess Union2.first(First f) would be shorter and nicer. If that does 
not work than maybe Union2.createFirst(First f) is in my opinion still better 
than Union2.union2first...
Comment 11 Jesse Glick 2006-06-05 16:30:58 UTC
Re. union2first: true, probably createFirst would be better. I guess I was
optimizing the naming for the case that you would be using a static import of
the method, as I originally was in the Ant module, but probably this is not the

BTW it seems Java type inference is pretty weak. This works:

Union2<A,B> x;
// ...
x = Union2.union2First(new A());

but this does not:

Set<Union2<A,B>> x;
// ...
x.add(Union2.union2First(new A()));

You have to explicitly say:

x.add(Union2.<A,B>union2First(new A()));

Kind of annoying.
Comment 12 Jesse Glick 2006-06-06 15:42:02 UTC
committed 1.30 ant/nbproject/project.xml
committed 1.35
committed 1.24
removed   1.7  ant/src/org/apache/tools/ant/module/
committed 1.8  ant/src/org/apache/tools/ant/module/api/
committed 1.25 ant/src/org/apache/tools/ant/module/api/
committed 1.14 ant/src/org/apache/tools/ant/module/api/support/
committed 1.10 ant/src/org/apache/tools/ant/module/nodes/
committed 1.56 ant/src/org/apache/tools/ant/module/run/
removed   1.7  ant/test/unit/src/org/apache/tools/ant/module/
committed 1.9  core/bootstrap/nbproject/project.xml
committed 1.24 core/bootstrap/src/org/netbeans/
committed 1.3  core/bootstrap/src/org/netbeans/
committed 1.14 core/bootstrap/src/org/netbeans/
committed 1.16 core/bootstrap/src/org/netbeans/
committed 1.8  core/bootstrap/src/org/netbeans/
committed 1.9  core/bootstrap/src/org/netbeans/
committed 1.10 core/execution/nbproject/project.xml
committed 1.6 
committed 1.8  core/startup/nbproject/project.xml
committed 1.14 core/startup/src/org/netbeans/core/startup/
committed 1.11 core/startup/src/org/netbeans/core/startup/layers/
committed 1.7  openide/fs/nbproject/project.xml
committed 1.8  openide/fs/src/org/openide/filesystems/
committed 1.18 openide/fs/src/org/openide/filesystems/
committed 1.5  openide/fs/src/org/openide/filesystems/
committed 1.5  openide/fs/src/org/openide/filesystems/
committed 1.14 openide/util/apichanges.xml
committed 1.15 openide/util/nbproject/
committed 1.14 openide/util/src/org/openide/util/
committed 1.2  openide/util/src/org/openide/util/
committed 1.2  openide/util/src/org/openide/util/
committed 1.2  openide/util/test/unit/src/org/openide/util/
committed 1.2  openide/util/test/unit/src/org/openide/util/