    001/*******************************************************************************
    002 * Copyright (c) 2016 Diamond Light Source Ltd. and others.
    003 * All rights reserved. This program and the accompanying materials
    004 * are made available under the terms of the Eclipse Public License v1.0
    005 * which accompanies this distribution, and is available at
    006 * http://www.eclipse.org/legal/epl-v10.html
    007 *
    008 * Contributors:
    009 *     Diamond Light Source Ltd - initial API and implementation
    010 *******************************************************************************/
    011package org.eclipse.january.metadata;
    012
    013import java.lang.reflect.Method;
    014import java.util.HashMap;
    015import java.util.Map;
    016
    017import org.eclipse.january.MetadataException;
    018import org.eclipse.january.dataset.LazyDatasetBase;
    019import org.eclipse.january.metadata.internal.AxesMetadataImpl;
    020import org.eclipse.january.metadata.internal.DimensionMetadataImpl;
    021import org.eclipse.january.metadata.internal.ErrorMetadataImpl;
    022import org.eclipse.january.metadata.internal.MaskMetadataImpl;
    023import org.eclipse.january.metadata.internal.OriginMetadataImpl;
    024import org.eclipse.january.metadata.internal.PeemMetadataImpl;
    025import org.eclipse.january.metadata.internal.StatisticsMetadataImpl;
    026
    027public class MetadataFactory {
    028
    029        private MetadataFactory() {
    030        }
    031
    032        static Map<Class<? extends MetadataType>, Class<? extends MetadataType>> metadataClasses = createMap();
    033        private static Map<Class<? extends MetadataType>, Class<? extends MetadataType>> createMap() {
    034                Map<Class<? extends MetadataType>, Class<? extends MetadataType>> map = new HashMap<Class<? extends MetadataType>, Class<? extends MetadataType>>();
    035                map.put(IMetadata.class, Metadata.class);
    036                map.put(ErrorMetadata.class, ErrorMetadataImpl.class);
    037                map.put(AxesMetadata.class, AxesMetadataImpl.class);
    038                map.put(DimensionMetadata.class, DimensionMetadataImpl.class);
    039                map.put(MaskMetadata.class, MaskMetadataImpl.class);
    040                map.put(OriginMetadata.class, OriginMetadataImpl.class);
    041                map.put(PeemMetadata.class, PeemMetadataImpl.class);
    042                map.put(StatisticsMetadata.class, StatisticsMetadataImpl.class);
    043                return map;
    044        }
    045
    046
    047        /**
    048         * Create a metadata object of given class with given arguments
    049         * @param clazz
    050         * @param arguments these must match the arguments given in the initialize method specified in the class
    051         * @return metadata object or null if type not available
    052         * @throws MetadataException if the class cannot be instantiated, the initialize method does not exist, or
    053         *  the initialize method could not be called successfully
    054         */
    055        public static <T extends MetadataType> T createMetadata(Class<T> clazz, Object... arguments) throws MetadataException {
    056                @SuppressWarnings("unchecked")
    057                Class<T> mdClass = (Class<T>) metadataClasses.get(clazz);
    058                if (mdClass == null) {
    059                        return null;
    060                }
    061
    062                T obj = null;
    063                try {
    064                        obj = mdClass.newInstance();
    065                } catch (Exception e) {
    066                        throw new MetadataException("Could not create metadata object", e);
    067                }
    068
    069                try {
    070                        for (Method m : mdClass.getMethods()) {
    071                                if (m.getName().equals("initialize")) {
    072                                        m.invoke(obj, arguments);
    073                                        return obj;
    074                                }
    075                        }
    076                } catch (Exception e) {
    077                        throw new MetadataException("Could not initialize object", e);
    078                }
    079                throw new MetadataException("Could not find initialize method");
    080        }
    081
    082        /**
    083         * Register metadata class
    084         * @param clazz
    085         */
    086        public static <T extends MetadataType> void registerClass(Class<T> clazz) {
    087                Class<? extends MetadataType> iClass = LazyDatasetBase.findMetadataTypeSubInterfaces(clazz);
    088
    089                metadataClasses.put(iClass, clazz);
    090        }
    091}