001package org.clafer.instance; 002 003import java.io.IOException; 004import java.util.ArrayList; 005import java.util.List; 006import org.clafer.ast.AstConcreteClafer; 007import org.clafer.common.Check; 008 009/** 010 * 011 * @author jimmy 012 */ 013public class InstanceModel { 014 015 private final InstanceClafer[] topClafers; 016 017 public InstanceModel(InstanceClafer... topClafers) { 018 this.topClafers = Check.noNulls(topClafers); 019 } 020 021 public InstanceClafer[] getTopClafers() { 022 return topClafers; 023 } 024 025 public InstanceClafer[] getTopClafers(AstConcreteClafer type) { 026 List<InstanceClafer> typedTopClafers = new ArrayList<>(); 027 for(InstanceClafer topClafer : topClafers) { 028 if(type.equals(topClafer.getType())) { 029 typedTopClafers.add(topClafer); 030 } 031 } 032 return typedTopClafers.toArray(new InstanceClafer[typedTopClafers.size()]); 033 } 034 035 /** 036 * Print solution to stdout. 037 */ 038 public void print() throws IOException { 039 print(System.out); 040 } 041 042 /** 043 * Print solution. 044 */ 045 public void print(Appendable out) throws IOException { 046 for (InstanceClafer top : topClafers) { 047 top.print(out); 048 } 049 } 050 051 @Override 052 public String toString() { 053 StringBuilder result = new StringBuilder(); 054 try { 055 print(result); 056 } catch (IOException e) { 057 // StringBuilder should not throw an IOException. 058 throw new Error(e); 059 } 060 return result.toString(); 061 } 062}