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

ratpack.registry.internal.DefaultRegistryBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * 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 ratpack.registry.internal;

import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;
import ratpack.func.Factory;
import ratpack.registry.Registry;
import ratpack.registry.RegistryBuilder;

import static ratpack.registry.Registries.join;

public class DefaultRegistryBuilder implements RegistryBuilder {

  private final ImmutableList.Builder> builder = ImmutableList.builder();
  private int size;

  @Override
  public int size() {
    return size;
  }

  @Override
  public  RegistryBuilder add(Class type, O object) {
    return add(TypeToken.of(type), object);
  }

  @Override
  public  RegistryBuilder add(TypeToken type, O object) {
    builder.add(new DefaultRegistryEntry<>(type, object));
    ++size;
    return this;
  }

  @Override
  public RegistryBuilder add(Object object) {
    @SuppressWarnings("unchecked")
    Class type = (Class) object.getClass();
    ++size;
    return add(type, object);
  }

  @Override
  public  RegistryBuilder add(Class type, Factory factory) {
    return add(TypeToken.of(type), factory);
  }

  @Override
  public  RegistryBuilder add(TypeToken type, Factory factory) {
    builder.add(new LazyRegistryEntry<>(type, factory));
    ++size;
    return this;
  }

  @Override
  public Registry build() {
    ImmutableList> entries = builder.build();
    if (entries.size() == 1) {
      return new CachingRegistry(new SingleEntryRegistry(entries.get(0)));
    } else {
      return new CachingRegistry(new MultiEntryRegistry(entries));
    }
  }

  @Override
  public Registry build(Registry parent) {
    return join(parent, build());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy