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

com.quhaodian.server.HandleEntry Maven / Gradle / Ivy

/*
 * Copyright (C) 2011 ritwik.net
 *
 * 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 com.quhaodian.server;

import com.quhaodian.commons.TypeChecker;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class HandleEntry {
  
  private final T handler;
  private final Map signatures;
  private final Set methods;
  
  public HandleEntry(TypeChecker typeChecker, T handler, Class... classes) {
    if (handler == null) {
      throw new NullPointerException("handler");
    }
    
    if (classes.length == 0) {
      throw new IllegalArgumentException(
          "at least one interface has to be mentioned");
    }
    
    this.handler = handler;
    
    Map> map = new HashMap>();
    Set set = new HashSet();
    
    for (Class clazz : classes) {
      typeChecker.isValidInterface(clazz, true);
      
      if (!clazz.isInterface()) {
        throw new IllegalArgumentException(
            "class should be an interface : " + clazz);
      }
      
      for (Method m : clazz.getMethods()) {
        set.add(m);
        Class[] params = m.getParameterTypes();
        
        List list = map.get(m.getName());
        if (list == null) {
          list = new ArrayList();
        }
        StringBuffer buff = new StringBuffer(typeChecker.getTypeName(m
            .getReturnType()));
        for (int i = 0; i < params.length; i++) {
          buff.append(",").append(typeChecker.getTypeName(params[i]));
        }
        list.add(buff.toString());
        map.put(m.getName(), list);
      }
      
    }
    
    Map signs = new TreeMap();
    for (Map.Entry> e : map.entrySet()) {
      String[] arr = new String[e.getValue().size()];
      signs.put(e.getKey(), e.getValue().toArray(arr));
    }
    
    this.methods = Collections.unmodifiableSet(set);
    this.signatures = Collections.unmodifiableMap(signs);
  }
  
  public T getHandler() {
    return handler;
  }
  
  public Map getSignatures() {
    return signatures;
  }
  
  public Set getMethods() {
    return methods;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy