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

org.kurento.modulecreator.definition.Method Maven / Gradle / Ivy

Go to download

Tool that generates code for RPC between the Kurento Media Server and remote libraries.

There is a newer version: 7.1.0
Show newest version
/*
 * (C) Copyright 2016 Kurento (http://kurento.org/)
 *
 * 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 org.kurento.modulecreator.definition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.annotations.SerializedName;

public class Method extends NamedElement {

  private List params;

  @SerializedName("return")
  private Return returnProp;

  public Method(String name, String doc, List params, Return returnProp) {
    super(name, doc);
    this.setParams(params);

    this.returnProp = returnProp;
  }

  public List getParams() {
    return params;
  }

  public Return getReturn() {
    return returnProp;
  }

  public void setParams(List params) {
    this.params = params;
  }

  public void setReturnProp(Return returnProp) {
    this.returnProp = returnProp;
  }

  @Override
  public String toString() {
    return "Method [params=" + params + ", return=" + returnProp + ", doc=" + getDoc() + ", name="
        + getName() + "]";
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = super.hashCode();
    result = prime * result + ((params == null) ? 0 : params.hashCode());
    result = prime * result + ((returnProp == null) ? 0 : returnProp.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (!super.equals(obj)) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    Method other = (Method) obj;
    if (params == null) {
      if (other.params != null) {
        return false;
      }
    } else if (!params.equals(other.params)) {
      return false;
    }
    if (returnProp == null) {
      if (other.returnProp != null) {
        return false;
      }
    } else if (!returnProp.equals(other.returnProp)) {
      return false;
    }
    return true;
  }

  @Override
  public List getChildren() {
    List children = new ArrayList();

    if (params != null) {
      children.addAll(params);
    }

    if (returnProp != null) {
      children.add(returnProp);
    }

    return children;
  }

  public List expandIfOpsParams() {

    boolean optParam = false;
    for (Param param : this.params) {
      if (param.isOptional()) {
        optParam = true;
        break;
      }
    }

    if (optParam) {

      List expandedMethods = new ArrayList();
      for (Param param : this.params) {
        if (param.isOptional()) {

          List newParams = sublistUntilParam(params, param);
          expandedMethods.add(new Method(name, doc, newParams, returnProp));
        }
      }

      return expandedMethods;

    } else {
      return Collections.emptyList();
    }
  }

  private List sublistUntilParam(List params, Param centinelParam) {

    List newParams = new ArrayList();

    for (Param param : params) {
      if (param != centinelParam) {
        newParams.add(param);
      } else {
        break;
      }
    }
    return newParams;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy