| 1 | package PIANOS.datastructures; |
| 2 | |
| 3 | import PIANOS.exceptions.*; |
| 4 | |
| 5 | import java.util.*; |
| 6 | |
| 7 | // import PIANOS.datastructures.Variable; |
| 8 | |
| 9 | public abstract class Distribution |
| 10 | { |
| 11 | int numberOfParameters; |
| 12 | int[] intParameter; |
| 13 | double[] realParameter; |
| 14 | Variable[] variableParameter; |
| 15 | int[] parameterType; |
| 16 | String[] parameterString; |
| 17 | |
| 18 | |
| 19 | public Distribution(int numberOfParameters) |
| 20 | { |
| 21 | this.numberOfParameters = numberOfParameters; |
| 22 | intParameter = new int[numberOfParameters]; |
| 23 | realParameter = new double[numberOfParameters]; |
| 24 | variableParameter = new Variable[numberOfParameters]; |
| 25 | parameterType = new int[numberOfParameters]; |
| 26 | parameterString = new String[numberOfParameters]; |
| 27 | } |
| 28 | |
| 29 | public int getNumberOfParameters() |
| 30 | { |
| 31 | return numberOfParameters; |
| 32 | } |
| 33 | |
| 34 | public boolean isInteger(int index) throws IndexOutOfBoundsException, NullPointerException |
| 35 | { |
| 36 | // Assumptions: |
| 37 | // 1) This object is linked, that is, arrays |
| 38 | // intParameter, realParameter, variableParameter |
| 39 | // contain the necessary data and links |
| 40 | |
| 41 | if (index < 0 || index >= numberOfParameters) |
| 42 | { |
| 43 | throw new IndexOutOfBoundsException(); |
| 44 | } |
| 45 | |
| 46 | assert parameterType[index] <= 2; |
| 47 | assert parameterType[index] >= 0; |
| 48 | |
| 49 | switch (parameterType[index]) |
| 50 | { |
| 51 | case 0: // integer |
| 52 | return true; |
| 53 | case 1: // double |
| 54 | return false; |
| 55 | case 2: // Variable |
| 56 | // the "return"-line can generate a NullPointerException, so... |
| 57 | if (variableParameter[index] == null) |
| 58 | { |
| 59 | throw new NullPointerException(); |
| 60 | } |
| 61 | return variableParameter[index].isInteger(); |
| 62 | } |
| 63 | // This should not be reached. |
| 64 | assert false; |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | public Object getParameter(int index) |
| 70 | { |
| 71 | // Assumptions: |
| 72 | // 1) This object is linked, that is, arrays |
| 73 | // intParameter, realParameter, variableParameter |
| 74 | // contain the necessary data and links |
| 75 | |
| 76 | if (index < 0 || index >= numberOfParameters) |
| 77 | { |
| 78 | throw new IndexOutOfBoundsException(); |
| 79 | } |
| 80 | |
| 81 | assert parameterType[index] <= 2; |
| 82 | assert parameterType[index] >= 0; |
| 83 | |
| 84 | switch (parameterType[index]) |
| 85 | { |
| 86 | case 0: // integer |
| 87 | return intParameter[index]; |
| 88 | case 1: // double |
| 89 | return realParameter[index]; |
| 90 | case 2: // Variable |
| 91 | // the "return"-line can generate a NullPointerException, so... |
| 92 | if (variableParameter[index] == null) |
| 93 | { |
| 94 | throw new NullPointerException(); |
| 95 | } |
| 96 | return variableParameter[index]; |
| 97 | } |
| 98 | // This should not be reached. |
| 99 | assert false; |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | public void setParameter(int index, int parameter) |
| 104 | { |
| 105 | if (index < 0 || index >= numberOfParameters) |
| 106 | { |
| 107 | throw new IndexOutOfBoundsException(); |
| 108 | } |
| 109 | |
| 110 | parameterType[index]=0; |
| 111 | intParameter[index]=parameter; |
| 112 | } |
| 113 | |
| 114 | public void setParameter(int index, double parameter) |
| 115 | { |
| 116 | if (index < 0 || index >= numberOfParameters) |
| 117 | { |
| 118 | throw new IndexOutOfBoundsException(); |
| 119 | } |
| 120 | |
| 121 | parameterType[index]=1; |
| 122 | realParameter[index]=parameter; |
| 123 | } |
| 124 | |
| 125 | public void setParameter(int index, Variable parameter) |
| 126 | { |
| 127 | if (index < 0 || index >= numberOfParameters) |
| 128 | { |
| 129 | throw new IndexOutOfBoundsException(); |
| 130 | } |
| 131 | |
| 132 | parameterType[index]=2; |
| 133 | variableParameter[index]=parameter; |
| 134 | } |
| 135 | |
| 136 | public String getParameterString(int index) |
| 137 | { |
| 138 | if (index < 0 || index >= numberOfParameters) |
| 139 | { |
| 140 | throw new IndexOutOfBoundsException(); |
| 141 | } |
| 142 | |
| 143 | return parameterString[index]; |
| 144 | } |
| 145 | |
| 146 | public void setParameterString(int index, String parameter) |
| 147 | { |
| 148 | if (index < 0 || index >= numberOfParameters) |
| 149 | { |
| 150 | throw new IndexOutOfBoundsException(); |
| 151 | } |
| 152 | |
| 153 | parameterString[index]=parameter; |
| 154 | } |
| 155 | |
| 156 | public String toString(){ |
| 157 | String toReturn = this.getClass().getName(); |
| 158 | if (toReturn.startsWith("PIANOS.datastructures.")) |
| 159 | toReturn = toReturn.substring(22); |
| 160 | if (parameterString == null) |
| 161 | return toReturn; |
| 162 | toReturn += "("; |
| 163 | |
| 164 | for (int i = 0; i < numberOfParameters; ++i){ |
| 165 | toReturn += parameterString[i]; |
| 166 | if (i + 1 != numberOfParameters) |
| 167 | toReturn += ", "; |
| 168 | } |
| 169 | toReturn += ")"; |
| 170 | |
| 171 | return toReturn; |
| 172 | } |
| 173 | |
| 174 | public abstract ArrayList<String> getIntroduction() throws IllegalParametersException; |
| 175 | |
| 176 | public abstract ArrayList<String> getGenCode(String[] parameters) throws IllegalParametersException, MissingFunctionException; |
| 177 | |
| 178 | public abstract ArrayList<String> getFreqCode(String[] parameters) throws IllegalParametersException, MissingFunctionException; |
| 179 | |
| 180 | } |