CService.java

  1. /*
  2.  * Copyright 2016 Providence Authors
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.providence.reflect.contained;

  22. import net.morimekta.providence.descriptor.PService;
  23. import net.morimekta.providence.descriptor.PServiceProvider;
  24. import net.morimekta.util.collect.UnmodifiableList;
  25. import net.morimekta.util.collect.UnmodifiableMap;

  26. import javax.annotation.Nonnull;
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;

  33. /**
  34.  * Descriptor for a complete service.
  35.  */
  36. public class CService extends PService implements CAnnotatedDescriptor {
  37.     private final Map<String, String> annotations;
  38.     private String                    documentation;

  39.     @SuppressWarnings("unchecked")
  40.     public CService(String documentation,
  41.                     String programName,
  42.                     String name,
  43.                     PServiceProvider extendsService,
  44.                     Collection<CServiceMethod> methods,
  45.                     Map<String, String> annotations) {
  46.         super(programName, name, extendsService, methods);
  47.         this.documentation = documentation;
  48.         this.annotations = annotations == null
  49.                            ? Collections.EMPTY_MAP
  50.                            : UnmodifiableMap.copyOf(annotations);
  51.     }

  52.     @Nonnull
  53.     @SuppressWarnings("unchecked")
  54.     public Collection<CServiceMethod> getMethods() {
  55.         return (Collection<CServiceMethod>) super.getMethods();
  56.     }

  57.     @Override
  58.     public CServiceMethod getMethod(String name) {
  59.         for (CServiceMethod method : getMethods()) {
  60.             if (method.getName().equals(name)) {
  61.                 return method;
  62.             }
  63.         }
  64.         if (getExtendsService() != null) {
  65.             return getExtendsService().getMethod(name);
  66.         }
  67.         return null;
  68.     }

  69.     @Override
  70.     public CService getExtendsService() {
  71.         return (CService) super.getExtendsService();
  72.     }

  73.     /**
  74.      * Get all methods including methods declared in extended services.
  75.      *
  76.      * @return The list of service methods.
  77.      */
  78.     public Collection<CServiceMethod> getMethodsIncludingExtended() {
  79.         CService extended = getExtendsService();
  80.         if (extended == null) {
  81.             return getMethods();
  82.         }
  83.         List<CServiceMethod> out = new ArrayList<>();
  84.         out.addAll(extended.getMethodsIncludingExtended());
  85.         out.addAll(getMethods());
  86.         return UnmodifiableList.copyOf(out);
  87.     }

  88.     @Nonnull
  89.     @Override
  90.     public Set<String> getAnnotations() {
  91.         return annotations.keySet();
  92.     }

  93.     @Override
  94.     public boolean hasAnnotation(@Nonnull String name) {
  95.         return annotations.containsKey(name);
  96.     }

  97.     @Override
  98.     public String getAnnotationValue(@Nonnull String name) {
  99.         return annotations.get(name);
  100.     }

  101.     @Override
  102.     public String getDocumentation() {
  103.         return documentation;
  104.     }
  105. }