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

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

There is a newer version: 0.6.0
Show 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import br.com.objectos.way.testable.Equality;
import br.com.objectos.way.testable.Testable;
import br.com.objectos.way.testable.Tester;

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

  private static final InterfaceInfoMap EMPTY_MAP = new InterfaceInfoMap(Collections. emptyList());

  private final List list;
  private final Map qualifiedNameMap;

  private InterfaceInfoMap(List list) {
    this.list = list;
    qualifiedNameMap = list.stream().collect(Collectors.toMap((InterfaceInfo i) -> i.qualifiedName(), i -> i));
  }

  public static InterfaceInfoMap fromTypeInfoList(List list) {
    List interfaceInfoList = list.stream()
        .map(TypeInfo::toInterfaceInfo)
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());
    return mapOf(interfaceInfoList);
  }

  public static InterfaceInfoMap mapOf() {
    return EMPTY_MAP;
  }

  public static InterfaceInfoMap mapOf(InterfaceInfo... interfaces) {
    return mapOf(Arrays.asList(interfaces));
  }

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

  public Optional get(Class type) {
    String name = type.getName();
    return get(name);
  }

  public Optional get(String qualifiedName) {
    InterfaceInfo maybe = null;

    if (qualifiedNameMap.containsKey(qualifiedName)) {
      maybe = qualifiedNameMap.get(qualifiedName);
    }

    return Optional.ofNullable(maybe);
  }

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

  public boolean contains(String qualifiedName) {
    return qualifiedNameMap.containsKey(qualifiedName);
  }

  @Override
  public Equality isEqualTo(Object that) {
    return Tester.of(InterfaceInfoMap.class)
        .add("list", o -> o.list)
        .test(this, that);
  }

  public InterfaceInfoMap add(InterfaceInfoMap otherMap) {
    List add = new ArrayList<>();
    add.addAll(list);
    add.addAll(otherMap.list);
    return InterfaceInfoMap.mapOf(add);
  }

  public Stream annotationInfoStream() {
    return list.stream()
        .flatMap(InterfaceInfo::annotationInfoStream);
  }

  public Optional first() {
    InterfaceInfo maybe = null;

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

    return Optional.ofNullable(maybe);
  }

  public List list() {
    return list;
  }

  public Stream methodInfoStream() {
    return list.stream()
        .flatMap(InterfaceInfo::methodInfoStream);
  }

  public Stream stream() {
    return list.stream();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy