Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. /*
  2.  * This file is part of Sponge, licensed under the MIT License (MIT).
  3.  *
  4.  * Copyright (c) SpongePowered <https://www.spongepowered.org>
  5.  * Copyright (c) contributors
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in
  15.  * all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23.  * THE SOFTWARE.
  24.  */
  25. package org.spongepowered.api.data.manipulator.immutable.common;
  26.  
  27. import static com.google.common.base.Preconditions.checkArgument;
  28. import static com.google.common.base.Preconditions.checkNotNull;
  29.  
  30. import org.spongepowered.api.CatalogType;
  31. import org.spongepowered.api.Sponge;
  32. import org.spongepowered.api.data.DataContainer;
  33. import org.spongepowered.api.data.key.Key;
  34. import org.spongepowered.api.data.manipulator.immutable.ImmutableVariantData;
  35. import org.spongepowered.api.data.manipulator.mutable.VariantData;
  36. import org.spongepowered.api.data.value.BaseValue;
  37. import org.spongepowered.api.data.value.immutable.ImmutableValue;
  38. import org.spongepowered.api.data.value.mutable.Value;
  39.  
  40. import java.lang.reflect.Modifier;
  41.  
  42. public abstract class AbstractImmutableSingleCatalogData<E extends CatalogType, I extends ImmutableVariantData<E, I, M>,
  43.         M extends VariantData<E, M, I>> extends AbstractImmutableSingleData<E, I, M> implements ImmutableVariantData<E, I, M> {
  44.  
  45.     private final Class<? extends M> mutableClass;
  46.     private final E defaultValue;
  47.     private final ImmutableValue<E> immutableValue;
  48.  
  49.     public AbstractImmutableSingleCatalogData(Class<I> immutableClass, E value, E defaultValue, Key<? extends BaseValue<E>> usedKey, Class<? extends M> mutableClass) {
  50.         super(immutableClass, value, usedKey);
  51.         checkArgument(!Modifier.isAbstract(mutableClass.getModifiers()), "The immutable class cannot be abstract!");
  52.         checkArgument(!Modifier.isInterface(mutableClass.getModifiers()), "The immutable class cannot be an interface!");
  53.         this.mutableClass = checkNotNull(mutableClass);
  54.         this.defaultValue = checkNotNull(defaultValue, "The default value was null! This is unacceptable! Maybe the value was not registered?");
  55.         this.immutableValue = Sponge.getRegistry().getValueFactory().createValue((Key<Value<E>>) (Key) this.usedKey, this.defaultValue, this.value).asImmutable();
  56.  
  57.     }
  58.  
  59.     @Override
  60.     public int compareTo(I o) {
  61.         return o.get(this.usedKey).get().getId().compareToIgnoreCase(this.getValue().getId());
  62.     }
  63.  
  64.     @Override
  65.     protected ImmutableValue<E> getValueGetter() {
  66.         return this.immutableValue;
  67.     }
  68.  
  69.     @Override
  70.     public M asMutable() {
  71.         return ReflectionUtil.createInstance(this.mutableClass, this.getValue());
  72.     }
  73.  
  74.     @Override
  75.     public DataContainer toContainer() {
  76.         return super.toContainer().set(this.usedKey.getQuery(), this.value.getId());
  77.     }
  78.  
  79.     @Override
  80.     public ImmutableValue<E> type() {
  81.         return this.immutableValue;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement