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

org.microbean.lang.Modeler Maven / Gradle / Ivy

There is a newer version: 0.0.20
Show newest version
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
 *
 * Copyright © 2022–2023 microBean™.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.microbean.lang;

import java.util.HashMap;
import java.util.Map;

import java.util.function.BiConsumer;
import java.util.function.Supplier;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;

import javax.lang.model.type.TypeMirror;

// NOT thread safe
public abstract class Modeler implements TypeAndElementSource {

  private final Map annotations;

  private final Map elements;

  private final Map types;

  protected Modeler() {
    super();
    this.annotations = new HashMap<>();
    this.elements = new HashMap<>();
    this.types = new HashMap<>();
  }

  @SuppressWarnings("unchecked")
  protected final  A annotation(final K k,
                                                               final Supplier s,
                                                               final BiConsumer c) {
    A r = (A)this.annotations.get(k);
    if (r == null) {
      final A a = s.get();
      r = (A)this.annotations.putIfAbsent(k, a);
      if (r == null) {
        c.accept(k, a);
        return a;
      }
    }
    return r;
  }

  @SuppressWarnings("unchecked")
  protected final  E element(final K k,
                                                   final Supplier s,
                                                   final BiConsumer c) {
    E r = (E)this.elements.get(k);
    if (r == null) {
      final E e = s.get();
      r = (E)this.elements.putIfAbsent(k, e);
      if (r == null) {
        c.accept(k, e);
        return e;
      }
    }
    return r;
  }

  @SuppressWarnings("unchecked")
  protected final  T type(final K k,
                                                   final Supplier s,
                                                   final BiConsumer c) {
    T r = (T)this.types.get(k);
    if (r == null) {
      final T t = s.get();
      r = (T)this.types.putIfAbsent(k, t);
      if (r == null) {
        c.accept(k, t);
        return t;
      }
    }
    return r;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy