001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest;
014
015import java.util.*;
016
017import org.apache.juneau.annotation.*;
018
019/**
020 * TODO
021 *
022 */
023@Bean(bpi="resource,methods")
024@SuppressWarnings("javadoc")
025public class StatusStats implements Comparable<StatusStats> {
026
027   private final Class<?> resource;
028   private final Map<java.lang.reflect.Method,StatusStats.Method> methods = new TreeMap<>();
029
030   public StatusStats(Class<?> resource) {
031      this.resource = resource;
032   }
033
034   public static StatusStats create(Class<?> resource) {
035      return new StatusStats(resource);
036   }
037
038   public Class<?> getResource() {
039      return resource;
040   }
041
042   public StatusStats.Method getMethod(java.lang.reflect.Method method) {
043      StatusStats.Method m = methods.get(method);
044      if (m == null) {
045         m = new Method(method);
046         methods.put(method, m);
047      }
048      return m;
049   }
050
051   public Set<StatusStats.Method> getMethods() {
052      return new TreeSet<>(methods.values());
053   }
054
055   @Bean(bpi="method,codes")
056   public static class Method implements Comparable<Method> {
057      private java.lang.reflect.Method method;
058      private Set<Status> codes = new TreeSet<>();
059
060      Method(java.lang.reflect.Method method) {
061         this.method = method;
062      }
063
064      public Method status(int code, int count) {
065         codes.add(new Status(code, count));
066         return this;
067      }
068
069      @Override
070      public int compareTo(Method o) {
071         return method.getName().compareTo(o.method.getName());
072      }
073   }
074
075   @Bean(bpi="code,count")
076   public static class Status implements Comparable<Status> {
077      private int code;
078      private int count;
079
080      public Status(int code, int count) {
081         this.code = code;
082         this.count = count;
083      }
084
085      @Override
086      public int compareTo(Status o) {
087         return Integer.compare(code, o.code);
088      }
089
090      public int getCount() {
091         return count;
092      }
093   }
094
095   @Override
096   public int compareTo(StatusStats o) {
097      return resource.getName().compareTo(o.resource.getName());
098   }
099}