@Override public String getSymbol(int code, OutputFormat mode) throws IndexConverterException { return converter.getSymbol(code, mode) + strokesString; }
@Override public byte getType() { return (byte) (IndexType.ALPHABETS_COUNT + (numberOfStrokes * converter.getType())); } }
@Override public int maxNumberOfSymbols() { return 10 * (innerConverter.maxNumberOfSymbols() + 1) - 1; } }
/** * Returns integer representation from specified string representation of single * index. * * @param index string representation of single index * @return integer representation of specified index * @throws IllegalArgumentException if rule for specified string */ public int getCode(String index) { try { for (IndexSymbolConverter converter : converters) if (converter.applicableToSymbol(index)) return (converter.getCode(index) & 0xFFFF) | ((converter.getType() & 0x7F) << 24); throw new IllegalArgumentException("No available converters for such symbol : " + index); } catch (IndexConverterException e) { throw new IllegalArgumentException("No available converters for such symbol : " + index); } } }
@Override public String getSymbol(int code, OutputFormat mode) throws IndexConverterException { int subscript = code / (innerConverter.maxNumberOfSymbols() + 1); if (subscript == 0) return innerConverter.getSymbol(code, mode); else { String symbol = innerConverter.getSymbol(code % (innerConverter.maxNumberOfSymbols() + 1), mode); if (mode.is(OutputFormat.WolframMathematica)) return "Subscript[" + symbol + ", " + subscript + "]"; if (mode.is(OutputFormat.Maple)) return symbol + subscript; return symbol + "_" + ("{" + subscript + "}"); } }
/** * Returns string representation from specified integer representation of single * index in the specified {@code outputFormat}. * * @param code integer representation of single index * @param outputFormat output format to be used to produce string representation * @return string representation of specified integer index * @throws IllegalArgumentException if rule for specified code */ public String getSymbol(int code, OutputFormat outputFormat) { byte typeId = (byte) ((code >>> 24) & 0x7F); int number = code & 0xFFFF; try { for (IndexSymbolConverter converter : converters) if (converter.getType() == typeId) { return converter.getSymbol(number, outputFormat);//symbol.length() == 1 ? symbol : symbol + " "; } throw new IllegalArgumentException("No appropriate converter for typeId 0x" + Integer.toHexString(typeId)); } catch (IndexConverterException e) { throw new IllegalArgumentException("Index 0x" + Integer.toHexString(code) + " conversion error"); } }
@Override public int getCode(String symbol) throws IndexConverterException { if (!symbol.contains("_")) return innerConverter.getCode(symbol); String[] split = symbol.split("_"); if (split.length != 2 || split[1].length() == 0) throw new IndexConverterException(); if (split[1].charAt(0) == '{') { if (split[1].length() < 3) throw new IndexConverterException(); split[1] = split[1].substring(1, split[1].length() - 1); } int num; try { num = Integer.parseInt(split[1]); } catch (NumberFormatException e) { throw new IndexConverterException(); } return (num) * (1 + innerConverter.maxNumberOfSymbols()) + innerConverter.getCode(split[0]); }
@Override public boolean applicableToSymbol(String symbol) { if (!symbol.contains("_")) return innerConverter.applicableToSymbol(symbol); String[] split = symbol.split("_"); if (split.length != 2 || split[1].length() == 0) return false; if (split[1].charAt(0) == '{') { if (split[1].length() < 3) return false; split[1] = split[1].substring(1, split[1].length() - 1); } try { Integer.parseInt(split[1]); return innerConverter.applicableToSymbol(split[0]); } catch (NumberFormatException e) { return false; } }
@Override public int getCode(String symbol) throws IndexConverterException { return converter.getCode(getBase(symbol)); }
@Override public boolean applicableToSymbol(String symbol) { if (symbol.length() <= strokesString.length()) return false; if (!strokesString.equals(getStrokes(symbol))) return false; return converter.applicableToSymbol(getBase(symbol)); }
/** * Returns the types bits corresponding to this index type. * * @return types bits corresponding to this index type. */ public byte getType() { return converter.getType(); }
@Override public int maxNumberOfSymbols() { return converter.maxNumberOfSymbols(); }
@Override public byte getType() { return innerConverter.getType(); }
/** * Creates {@link IndexSymbolConverter} for subscripted symbols from specified converter. * * @param converter converter to be extended with symbols with strokes * @param numberOfStrokes number of strokes to be appended after symbol */ public IndexWithStrokeConverter(IndexSymbolConverter converter, byte numberOfStrokes) { if (numberOfStrokes + converter.getType() > Byte.MAX_VALUE) throw new IllegalArgumentException("Too much strokes."); this.converter = converter; this.numberOfStrokes = numberOfStrokes; StringBuilder sb = new StringBuilder(); while (numberOfStrokes-- > 0) sb.append('\''); strokesString = sb.toString(); }
/** * Creates index converted manager for specified index converters. * * @param converters index converters * @throws IllegalArgumentException if several converters have the same index type */ public IndexConverterManager(IndexSymbolConverter[] converters) { TByteHashSet types = new TByteHashSet(converters.length); for (IndexSymbolConverter converter : converters) { if (types.contains(converter.getType())) throw new IllegalArgumentException("Several converters for same type."); types.add(converter.getType()); } this.converters = converters; }