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

org.immutables.criteria.backend.UniqueCachedNaming Maven / Gradle / Ivy

/*
 * Copyright 2019 Immutables Authors and Contributors
 *
 * 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.immutables.criteria.backend;

import com.google.common.base.Converter;
import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;

import java.util.Iterator;
import java.util.Objects;

/**
 * Gives an unique (generated) name to objects so they can be used as identifiers in queries.
 * Same objects (as defined by object equality) will have same name.
 *
 * @param 
 */
public class UniqueCachedNaming implements NamingStrategy {

  private static final Suggester PREFIX_SUGGESTER = (first, attempts, size) -> "expr" + size;

  private final BiMap names;

  private final Converter converter;

  private UniqueCachedNaming(Iterable values) {
    Objects.requireNonNull(values, "values");
    this.names = buildBiMap(values);
    this.converter =  new Converter() {
      @Override
      protected String doForward(T toName) {
        Preconditions.checkArgument(names.containsKey(toName), "%s was not cached", toName);
        return names.get(toName);
      }

      @Override
      protected T doBackward(String fromName) {
        Preconditions.checkArgument(names.containsValue(fromName), "name %s not found", fromName);
        return names.inverse().get(fromName);
      }
    };
  }

  private static  BiMap buildBiMap(Iterable values) {
    @SuppressWarnings("unchecked")
    Suggester suggester = (Suggester) PREFIX_SUGGESTER;
    final BiMap map = HashBiMap.create();
    for (T value: values) {
      if (!map.containsKey(value)) {
        String name;
        for (int i = 0; ; i++) {
          name = suggester.suggest(value, i, map.size());
          if (!map.containsValue(name)) {
            map.put(value, name);
            break; // name found, break the loop
          }
        }
      }
    }

    return ImmutableBiMap.copyOf(map);
  }

  public Converter asConverter() {
    return converter;
  }

  /**
   * Suggest a name for a value
   */
  private interface Suggester {
    String suggest(T value, int attempts, int size);
  }

  @Override
  public String name(T toName) {
    return asConverter().convert(toName);
  }

  public static  UniqueCachedNaming of(Iterable iterable) {
    return new UniqueCachedNaming<>(iterable);
  }

  public static  UniqueCachedNaming of(Iterator iterator) {
    return of(ImmutableList.copyOf(iterator));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy