001/* 002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon) 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 */ 009 010package org.eclipse.golo.compiler.ir; 011 012import java.util.Collection; 013import java.util.Set; 014import java.util.LinkedHashSet; 015import java.util.LinkedList; 016import org.eclipse.golo.compiler.PackageAndClass; 017import org.eclipse.golo.compiler.parser.GoloASTNode; 018import static java.util.Collections.unmodifiableSet; 019 020/** 021 * Named augmentation definition 022 */ 023public final class NamedAugmentation extends GoloElement implements FunctionContainer { 024 private final PackageAndClass name; 025 private final Set<GoloFunction> functions = new LinkedHashSet<>(); 026 027 NamedAugmentation(PackageAndClass name) { 028 super(); 029 this.name = name; 030 } 031 032 public String getName() { 033 return this.name.toString(); 034 } 035 036 public PackageAndClass getPackageAndClass() { 037 return this.name; 038 } 039 040 @Override 041 public NamedAugmentation ofAST(GoloASTNode node) { 042 super.ofAST(node); 043 return this; 044 } 045 046 @Override 047 public Set<GoloFunction> getFunctions() { 048 return unmodifiableSet(functions); 049 } 050 051 @Override 052 public void addFunction(GoloFunction func) { 053 functions.add(func); 054 makeParentOf(func); 055 } 056 057 @Override 058 public void addFunctions(Collection<GoloFunction> funcs) { 059 for (GoloFunction f : funcs) { 060 addFunction(f); 061 } 062 } 063 064 @Override 065 public boolean hasFunctions() { 066 return !functions.isEmpty(); 067 } 068 069 public NamedAugmentation add(Object fun) { 070 addFunction((GoloFunction) fun); 071 return this; 072 } 073 074 @Override 075 public void accept(GoloIrVisitor visitor) { 076 visitor.visitNamedAugmentation(this); 077 } 078 079 @Override 080 public void walk(GoloIrVisitor visitor) { 081 for (GoloFunction fun : new LinkedList<>(functions)) { 082 fun.accept(visitor); 083 } 084 } 085 086 @Override 087 protected void replaceElement(GoloElement original, GoloElement newElement) { 088 if (functions.contains(original)) { 089 functions.remove(original); 090 add(newElement); 091 } else { 092 throw cantReplace(original, newElement); 093 } 094 } 095}