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

com.almworks.jira.structure.api2g.attribute.SimpleAttributeProvider Maven / Gradle / Ivy

There is a newer version: 17.25.3
Show newest version
package com.almworks.jira.structure.api2g.attribute;

import com.almworks.jira.structure.api.column.AttributeContext;
import com.almworks.jira.structure.api.column.StructureColumnException;
import com.almworks.jira.structure.api2g.attribute.loader.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

// todo problem (STR-504): suppose we register loaders for foo:B, foo:C and we're asked for foo:A
// todo   where C can be converted to A, but B can't
// todo   we'll return loader foo:B, but should have returned foo:C
public abstract class SimpleAttributeProvider implements AttributeLoaderProvider {
  private final Map, AttributeLoader> myLoaders = new HashMap<>();
  // If requested format is not found, we return the first registered loader for the spec ignoring the format
  private final Map, AttributeLoader> myLoadersNormalized = new HashMap<>(); 

  // Call from constructor
  protected final void registerLoader(AttributeLoader loader) {
    AttributeSpec spec = loader.getAttributeSpec();
    if (myLoaders.containsKey(spec)) {
      throw new IllegalArgumentException("loader with spec " + spec + " is already registered");
    }
    myLoaders.put(spec, loader);
    
    AttributeSpec normalizedSpec = normalize(spec);
    if (!myLoadersNormalized.containsKey(normalizedSpec)) {
      myLoadersNormalized.put(normalizedSpec, loader);
    }
  }

  private static AttributeSpec normalize(AttributeSpec attributeSpec) {
    return attributeSpec.as(ValueFormat.HTML);
  }

  @SafeVarargs
  protected final  void registerCompositeLoader(AttributeSpec spec, AttributeLoader... loaders) {
    registerLoader(CompositeAttributeLoader.create(spec, Arrays.asList(loaders)));
  }

  @Nullable
  public synchronized AttributeLoader createAttributeLoader(AttributeSpec attributeSpec,
    @NotNull AttributeContext context) throws StructureColumnException
  {
    AttributeLoader r = myLoaders.get(attributeSpec);
    if (r == null) {
      attributeSpec = normalize(attributeSpec);
      r = myLoadersNormalized.get(attributeSpec);
    }
    return r;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy