blob: ce9741cf58e6d7880a0b0c0edb9b1e64bc54579d [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.
//
= DevFaqLookupNonSingleton
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqLookupNonSingleton
:description: Apache NetBeans wiki DevFaqLookupNonSingleton
:toc: left
:toc-title:
:syntax: true
=== If there is more than one of a type in a Lookup, which instance will I get?
As noted in link:DevFaqLookup.asciidoc[the overview of Lookup], a Lookup can contain more than one instance of a given class; Lookup is often used for singletons, but not exclusively for singletons. For example, in the link:http://www.netbeans.org/download/dev/javadoc/ProjectAPI/overview-summary.html[Projects API], there is a class called link:http://www.netbeans.org/download/dev/javadoc/ProjectAPI/org/netbeans/spi/project/ProjectFactory.html[ProjectFactory] that recognizes different types of user projects on disk; each module that provides a project type registers another factory in the system.
So the inevitable question is, if there are two instances of X in a Lookup, and I call `lookup(X.class)`, which one do I get?
The answer is, it's undefined - don't do that. The next inevitable question is, but how can that be?
A Lookup makes no assumptions about what's in it, or what you might want to put in it, or how many of anything there should be. That contract is an agreement between whoever tells you that you should get an instance of X from some Lookup and you. If they document that there will only be one, use `Lookup.lookup(Class)`. If they document that there can be more than one, use `Lookup.lookupAll(Class)` and iterate the results.
In practice this is a non-problem - anything you are going to try to find in a `Lookup` is going to document whether it is supposed to be a singleton or not.
=== 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/DevFaqLookupNonSingleton[http://wiki.netbeans.org/DevFaqLookupNonSingleton] ,
that was last modified by NetBeans user Tboudreau
on 2010-02-19T00:39:45Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.