/*************************************************************************** * * Returns the object bound to the given key. * * The search is performed by just looking at all the entries in the map; * since they are sorted in reversal hyerarchy depth it's ok to return the * first match (any further match would refer to superclasses). * **************************************************************************/ @Override @CheckForNull public T get (@Nonnull final Object key) { for (final Map.Entry<Class<?>, T> entry : entrySet()) { if (entry.getKey().isAssignableFrom((Class)key)) { return entry.getValue(); } } return null; } }
/*************************************************************************** * * **************************************************************************/ @Nonnull private ClassMap<List<MetadataItemHolder<?>>> findHolderMap (@Nonnull final StorageType storageType) { // Do not clone results, should return the live pointers ClassMap<List<MetadataItemHolder<?>>> result = null; if (storageType == StorageType.ANY_TYPE) { throw new IllegalArgumentException("storageType == ANY_TYPE, but it's not a real key"); } result = holderMapByOption.get(storageType); if (result == null) { result = new ClassMap<List<MetadataItemHolder<?>>>(); holderMapByOption.put(storageType, result); } return result; }
/*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Override public boolean isItemAvailable (@Nonnull final Class<?> metadataItemClass, @Nonnull final FindOption ... options) throws NoSuchMetadataTypeException { final StorageType storageType = MetadataSpiUtils.findOption(options, StorageType.DEFAULT, StorageType.class); final ClassMap<List<MetadataItemHolder<?>>> holderMapByClass = findHolderMap(storageType); return holderMapByClass.containsKey(metadataItemClass); }
@Test public void testInitialization() { logger.info("******** testInitialization()"); assertEquals(2, MetadataImpl.providerMapByClass.size()); assertEquals(3, MetadataImpl.interceptors.size()); assertTrue(MetadataImpl.providerMapByClass.get(MockItem1.class) instanceof MetadataItemProviderMock1); assertTrue(MetadataImpl.providerMapByClass.get(MockItem2.class) instanceof MetadataItemProviderMock2); assertTrue(MetadataImpl.interceptors.get(0) instanceof MetadataItemInterceptorMock1a); assertTrue(MetadataImpl.interceptors.get(1) instanceof MetadataItemInterceptorMock1b); assertTrue(MetadataImpl.interceptors.get(2) instanceof MetadataItemInterceptorMock2a); }
/*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Override public <Item> int storeItem (@Nonnull final MetadataItemHolder<Item> holder, @Nonnull final StoreOption ... options) throws NoSuchMetadataTypeException { logger.info("storeItem(%s, %s)", holder, Arrays.toString(options)); if (holder == null) { throw new IllegalArgumentException("holder is mandatory"); } MetadataSpiUtils.validateOptions(options); final Class<Item> itemClass = (Class<Item>)holder.get().getClass(); final MetadataItemProvider<Item> metadataProvider = (MetadataItemProvider<Item>)providerMapByClass.get(itemClass); if (metadataProvider == null) { throw new NoSuchMetadataTypeException(itemClass); } return metadataProvider.storeItem(getDataObject(), holder, options); }
/*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Nonnull @Override public Set<Class<?>> getItemClasses() { return new CopyOnWriteArraySet<Class<?>>(providerMapByClass.keySet()); }