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

br.com.objectos.way.code.InterfaceInfoMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.code;

import static com.google.common.collect.Lists.transform;

import java.util.List;
import java.util.Map;

import br.com.objectos.way.core.testing.Testable;
import br.com.objectos.way.core.testing.Testables;
import br.com.objectos.way.core.util.WayMap;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

/**
 * @author [email protected] (Marcio Endo)
 */
public class InterfaceInfoMap implements Testable, WayMap {

  private static final InterfaceInfoMap EMPTY_MAP = new InterfaceInfoMap(ImmutableList. of());

  private final List list;
  private final Map qualifiedNameMap;

  private InterfaceInfoMap(List list) {
    this.list = list;
    qualifiedNameMap = Maps.uniqueIndex(list, HasQualifiedNameToQualifiedName.get());
  }

  public static InterfaceInfoMap fromTypeInfoList(List list) {
    List> optionalList = transform(list, TypeInfoToInterfaceInfo.get());
    Iterable presentList = Optional.presentInstances(optionalList);
    List interfaceInfoList = ImmutableList.copyOf(presentList);
    return mapOf(interfaceInfoList);
  }

  public static InterfaceInfoMap mapOf() {
    return EMPTY_MAP;
  }

  public static InterfaceInfoMap mapOf(List list) {
    return new InterfaceInfoMap(list);
  }

  public Optional get(Class type) {
    InterfaceInfo maybe = null;

    String name = type.getName();
    if (qualifiedNameMap.containsKey(name)) {
      maybe = qualifiedNameMap.get(name);
    }

    return Optional.fromNullable(maybe);
  }

  public boolean contains(Class type) {
    String name = type.getName();
    return qualifiedNameMap.containsKey(name);
  }

  @Override
  public boolean isEqual(InterfaceInfoMap that) {
    return Testables.isEqualHelper()
        .equal(list, that.list)
        .result();
  }

  public InterfaceInfoMap add(InterfaceInfoMap otherMap) {
    List add = ImmutableList. builder()
        .addAll(list)
        .addAll(otherMap.list)
        .build();
    return InterfaceInfoMap.mapOf(add);
  }

  public Optional first() {
    InterfaceInfo maybe = null;

    if (list.size() > 0) {
      maybe = list.get(0);
    }

    return Optional.fromNullable(maybe);
  }

  @Override
  public List list() {
    return list;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy