blob: 27f4023fd84503b7afc04eee4421003aa4539027 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqLookupVsHashmap
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqLookupVsHashmap
:description: Apache NetBeans wiki DevFaqLookupVsHashmap
:toc: left
:toc-title:
:syntax: true
=== Why use this strange convention - wouldn't a Map be good enough?
Other platforms do use string-keyed maps for this sort of thing, but there are some weaknesses with that approach:
1. it is impossible to enforce link:DevFaqModuleDependencies.asciidoc[dependencies]. With `link:DevFaqLookup.asciidoc[Lookup]`, a module's code cannot request an object of type X unless it can load/access the `X.class`. The module trying to look up an X will not be able to see `X.class` unless it link:DevFaqModuleDependencies.asciidoc[declares a dependency] on the module that defines X _and_ the module which defines X says that it allows access to the Java package X lives in. (A `Map<Class<T>,T>` would do the same job as `Lookup`.)
2. The class of values in a map can change without notice - so if you have `(SomeIface) foo = (SomeIface) globalMap.get("foo")`, some new version of the module that provides "foo" can change the return type, causing `ClassCastException` s; with `Lookup`, you cannot ever get an object that is not of the type you passed in the request - so Lookup's approach is more robust.
3. `Lookup` supports listening to changes in the result.
4. `Lookup` supports multiple instances for one key - if you call `lookup(X.class)` you get one instance. If you call `lookupAll(X.class)` you get a `Collection<? extends X>` (so with `lookupAll()` it is more like a `Map<Class<T>,List<T>>`)
There are some other capabilities of `Lookup` (such as getting the specific type or number of results without actually creating objects, and providing named result items) but these are rarely used in practice.
`Lookup` is very powerful, yet simple and generic; people quickly learn to love it, once they realize what it can do.
See also the javadoc: [link:http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.html[http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.html] `org.openide.util.Lookup`]
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqLookupVsHashmap[http://wiki.netbeans.org/DevFaqLookupVsHashmap] ,
that was last modified by NetBeans user Tboudreau
on 2010-02-18T23:58:49Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.