org.apache.brooklyn.camp.spi.collection.BasicResourceLookup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camp-base Show documentation
Show all versions of camp-base Show documentation
Core base classes for CAMP server implementation
The newest version!
/*
* 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.brooklyn.camp.spi.collection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.brooklyn.camp.spi.AbstractResource;
import org.apache.brooklyn.util.collections.MutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BasicResourceLookup extends AbstractResourceLookup {
private static final Logger log = LoggerFactory.getLogger(BasicResourceLookup.class);
Map items = new MutableMap();
Map> links = new MutableMap>();
@Override
public T get(String id) {
return items.get(id);
}
@Override
public synchronized List> links() {
return new ArrayList>(links.values());
}
public synchronized void add(T item) {
if (items.containsKey(item.getId())) {
throw new IllegalStateException("Already contains item for "+item.getId()+" (adding "+item+")");
}
if (!items.isEmpty() && items.size()%100==0) {
// useful for monitoring any leaks here
log.debug("Creating new CAMP item in "+this+" (had "+(items.size())+"): "+item);
}
items.put(item.getId(), item);
links.put(item.getId(), newLink(item.getId(), item.getName()));
}
public synchronized void addAll(@SuppressWarnings("unchecked") T... items) {
for (T item: items) add(item);
}
public synchronized T update(T item) {
T old = items.put(item.getId(), item);
links.put(item.getId(), newLink(item.getId(), item.getName()));
return old;
}
public synchronized boolean remove(String id) {
items.remove(id);
return links.remove(id)!=null;
}
@SafeVarargs
public static BasicResourceLookup of(T ...items) {
BasicResourceLookup result = new BasicResourceLookup();
for (T item: items) result.add(item);
return result;
}
}