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

org.carrot2.attrs.AttrObjectArray Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2021, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * https://www.carrot2.org/carrot2.LICENSE
 */
package org.carrot2.attrs;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class AttrObjectArray extends Attr> {
  private Class clazz;
  private Supplier> getter;
  private Consumer> setter;
  private Supplier newEntryInstance;

  AttrObjectArray(
      Class clazz,
      List defaultValue,
      String label,
      List>> constraint,
      Supplier newEntryInstance,
      Supplier> getter,
      Consumer> setter) {
    super(null, label, constraint);
    this.clazz = clazz;

    this.setter = setter != null ? setter : super::set;
    this.getter = getter != null ? getter : super::get;
    this.newEntryInstance = newEntryInstance;

    set(defaultValue);
  }

  public void set(List value) {
    super.set(value);
    setter.accept(value);
  }

  public List get() {
    return getter.get();
  }

  public Class getInterfaceClass() {
    return clazz;
  }

  public boolean isDefaultClass(Object value) {
    Objects.requireNonNull(value);
    T def = newDefaultEntryValue();
    return def != null && Objects.equals(def.getClass(), value.getClass());
  }

  public T newDefaultEntryValue() {
    return this.newEntryInstance.get();
  }

  public static class Builder extends BuilderScaffold> {
    private final Supplier defaultEntryValue;
    private Class clazz;
    private Supplier> getter;
    private Consumer> setter;

    public Builder(Class clazz, Supplier defaultEntryValue) {
      this.clazz = clazz;
      this.defaultEntryValue = defaultEntryValue;
    }

    public Builder getset(Supplier> getter, Consumer> setter) {
      this.setter = Objects.requireNonNull(setter);
      this.getter = Objects.requireNonNull(getter);
      return this;
    }

    @Override
    public Builder label(String label) {
      super.label(label);
      return this;
    }

    public AttrObjectArray defaultValue(List defaultValue) {
      return new AttrObjectArray(
          clazz, defaultValue, label, getConstraint(), defaultEntryValue, getter, setter);
    }
  }

  public static  Builder builder(
      Class entryClazz, Supplier defaultEntryValue) {
    return new Builder<>(entryClazz, defaultEntryValue);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy