All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thaiopensource.util.SinglePropertyMap Maven / Gradle / Ivy

There is a newer version: 20220510
Show newest version
package com.thaiopensource.util;

public class SinglePropertyMap implements PropertyMap {
  private final PropertyId pid;
  private final T value;

  private SinglePropertyMap(PropertyId pid, T value) {
    if (!(pid.getValueClass().isInstance(value))) {
      if (value == null)
        throw new NullPointerException();
      throw new ClassCastException();
    }
    this.pid = pid;
    this.value = value;
  }

  public  V get(PropertyId pid) {
    if (pid != this.pid)
      return null;
    // it would be nice to avoid the cast,
    // but I can't figure out how to do it
    return pid.getValueClass().cast(value);
  }

  public boolean contains(PropertyId pid) {
    return pid == this.pid;
  }

  public int size() {
    return 1;
  }

  public PropertyId getKey(int i) {
    if (i != 0)
      throw new IndexOutOfBoundsException();
    return pid;
  }

  public static  SinglePropertyMap newInstance(PropertyId pid, T value) {
    return new SinglePropertyMap(pid, value);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy