![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.cp.BeanStoreEntry Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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. *
// ***************************************************************************************************************************
package org.apache.juneau.cp;
import static org.apache.juneau.collections.JsonMap.*;
import static org.apache.juneau.common.internal.ArgUtils.*;
import static org.apache.juneau.common.internal.StringUtils.*;
import static org.apache.juneau.internal.ClassUtils.*;
import java.util.function.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.internal.*;
/**
* Represents a bean in a {@link BeanStore}.
*
*
* A bean entry consists of the following:
*
* - A class type.
*
- A bean or bean supplier that returns an instance of the class type. This can be a subclass of the type.
*
- An optional name.
*
*
* @param The bean type.
*/
public class BeanStoreEntry {
//-----------------------------------------------------------------------------------------------------------------
// Static
//-----------------------------------------------------------------------------------------------------------------
/**
* Static creator.
*
* @param The class type to associate with the bean.
* @param type The class type to associate with the bean.
* @param bean The bean supplier.
* @param name Optional name to associate with the bean. Can be null .
* @return A new bean store entry.
*/
public static BeanStoreEntry create(Class type, Supplier bean, String name) {
return new BeanStoreEntry<>(type, bean, name);
}
//-----------------------------------------------------------------------------------------------------------------
// Instance
//-----------------------------------------------------------------------------------------------------------------
final Supplier bean;
final Class type;
final String name;
/**
* Constructor.
*
* @param type The class type to associate with the bean.
* @param bean The bean supplier.
* @param name Optional name to associate with the bean. Can be null .
*/
protected BeanStoreEntry(Class type, Supplier bean, String name) {
this.bean = assertArgNotNull("bean", bean);
this.type = assertArgNotNull("type", type);
this.name = nullIfEmpty(name);
}
/**
* Returns true if this bean is exactly of the specified type.
*
* @param type The class to check. Returns false if null .
* @return true if this bean is exactly of the specified type.
*/
public boolean matches(Class> type) {
return this.type.equals(type);
}
/**
* Returns true if this bean is exactly of the specified type and has the specified name.
*
* @param type The class to check. Returns false if null .
* @param name The name to check. Can be null to only match if name of entry is null .
* @return true if this bean is exactly of the specified type and has the specified name.
*/
public boolean matches(Class> type, String name) {
name = nullIfEmpty(name);
return matches(type) && StringUtils.eq(this.name, name);
}
/**
* Returns the bean associated with this entry.
*
* @return The bean associated with this entry.
*/
public T get() {
return bean.get();
}
/**
* Returns the type this bean is associated with.
*
* @return The type this bean is associated with.
*/
public Class getType() {
return type;
}
/**
* Returns the name associated with this entry.
*
* @return the name associated with this entry. null if no name is associated.
*/
public String getName() {
return name;
}
/**
* Returns the properties in this object as a simple map for debugging purposes.
*
* @return The properties in this object as a simple map.
*/
protected JsonMap properties() {
return filteredMap()
.append("type", simpleClassName(getType()))
.append("bean", ObjectUtils.identity(get()))
.append("name", getName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy