001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.meta.component;
018
019import net.tridentsdk.base.Block;
020import net.tridentsdk.base.Substance;
021import net.tridentsdk.registry.Registered;
022import net.tridentsdk.util.Value;
023
024import javax.annotation.concurrent.ThreadSafe;
025
026/**
027 * Allows Factory access to metadata components
028 *
029 * @author The TridentSDK Team
030 * @since 0.4-alpha
031 */
032@ThreadSafe
033public final class MetaFactory {
034    private static volatile MetaProvider provider;
035
036    /**
037     * Creates a new meta collection
038     *
039     * @return the new meta collection
040     */
041    public static <T extends MetaOwner> MetaCollection<T> newCollection() {
042        return new HashMetaCollection<>();
043    }
044
045    /**
046     * @see MetaProvider#decode(Block, net.tridentsdk.util.Value, byte[], net.tridentsdk.util.Value)
047     */
048    public static boolean decode(Block block, Value<Substance> substance, byte[] data, Value<Byte> result) {
049        ensureInit();
050        return provider.decode(block, substance, data, result);
051    }
052
053    /**
054     * Obtains a new instance of a meta value
055     *
056     * @param type the meta type class
057     * @param <S>  the meta owner type
058     * @param <T>  the meta value type
059     * @return the new meta value
060     */
061    public static <S, T extends Meta<S>> T newValue(Class<T> type) {
062        ensureInit();
063        return provider.provide(type);
064    }
065
066    /**
067     * Registers the meta value into the server meta registry
068     *
069     * @param meta the meta
070     */
071    public static void register(Meta meta) {
072        ensureInit();
073        provider.register(meta);
074    }
075
076    private static void ensureInit() {
077        if (provider == null) {
078            // hacky initializations
079            provider = Registered.impl().meta();
080        }
081    }
082}