001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package gololang.ir;
012
013import org.eclipse.golo.compiler.PackageAndClass;
014
015import java.util.List;
016import java.util.Collection;
017
018/**
019 * Interface for Golo elements that can contain functions (module, augmentations, ...).
020 */
021public interface FunctionContainer {
022  List<GoloFunction> getFunctions();
023
024  /**
025   * Adds a function to this container.
026   */
027  void addFunction(GoloFunction func);
028
029  /**
030   * Adds a function.
031   *
032   * @see #addFunction(GoloFunction)
033   */
034  default void addElement(Object elt) {
035    if (elt instanceof GoloFunction) {
036      addFunction((GoloFunction) elt);
037    } else if (!(elt instanceof Noop)) {
038      throw new IllegalArgumentException("Can't add a " + elt.getClass().getName());
039    }
040  }
041
042  PackageAndClass getPackageAndClass();
043
044  default void addFunctions(Collection<GoloFunction> funcs) {
045    for (GoloFunction f : funcs) {
046      addFunction(f);
047    }
048  }
049
050  default GoloFunction getFunction(GoloFunction function) {
051    for (GoloFunction f : getFunctions()) {
052      if (f.equals(function)) {
053        return f;
054      }
055    }
056    return null;
057  }
058
059  boolean hasFunctions();
060}