package com.harmoneye.tonecircle;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PitchClassSetNamer {

	private Map<Integer, Map<String, Object>> names = new HashMap<Integer, Map<String, Object>>();

	private static final String[] TONE_NAMES = { "C", "Db", "D", "Eb", "E",
		"F", "Gb", "G", "Ab", "A", "Bb", "B" };
	
	private PitchClassSetNamer() {

	}

	public static PitchClassSetNamer fromJson(InputStream inputStream) {
		PitchClassSetNamer namer = new PitchClassSetNamer();
		try {
			JsonFactory f = new JsonFactory();
			f.enable(JsonParser.Feature.ALLOW_COMMENTS);
			ObjectMapper objectMapper = new ObjectMapper(f);
			namer.names = objectMapper.readValue(inputStream,
				new TypeReference<Map<Integer, Map<String, Object>>>() {
				});
		} catch (Exception e) {
			Log.e("Namer", e.toString());
		}
		return namer;
	}

	public String getName(PitchClassSet tones) {
		Map<String, Object> details = names.get(tones.getCanonic().getIndex());
		if (details == null) {
			return "";
		}
		Integer templateRoot = (Integer) details.get("root");
		if (templateRoot == null) {
			templateRoot = 0;
		}
		int root = Modulo.modulo(templateRoot + tones.getRoot(), PitchClassSet.OCTAVE_SIZE);
		String rootName = TONE_NAMES[root];
		String chordName = (String) details.get("chord");
		if (chordName == null) {
			chordName = "";
		}
		chordName = chordName.replace("X", rootName);
		return chordName;
	}
}
@@ -77,63 +77,11 @@
 til.
-HashMap;%0Aimport java.util.Map;%0A%0Aimport android.util.Log
+Map
 ;%0A%0Ai
@@ -366,54 +366,8 @@
 ames
- = new HashMap%3CInteger, Map%3CString, Object%3E%3E()
 ;%0A%0A%09
@@ -480,17 +480,16 @@
  %22B%22 %7D;%0A
-%09
 %0A%09privat
@@ -513,16 +513,108 @@
 mer(
-) %7B%0A%0A%09%7D%0A
+Map%3CInteger, Map%3CString, Object%3E%3E names) %7B%0A%09%09this.names = names;%0A%09%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)
 %0A%09pu
@@ -684,63 +684,8 @@
 ) %7B%0A
-%09%09PitchClassSetNamer namer = new PitchClassSetNamer();%0A
 %09%09tr
@@ -833,19 +833,16 @@
 %0A%09%09%09
-namer.names
+Object n
  = o
@@ -951,84 +951,232 @@
 ;%0A%09%09
-%7D catch (Exception e) %7B%0A%09%09%09Log.e(%22Namer%22, e.toString());%0A%09%09%7D%0A%09%09return namer;
+%09return new PitchClassSetNamer((Map%3CInteger, Map%3CString, Object%3E%3E) n);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09if (e instanceof RuntimeException) %7B%0A%09%09%09%09throw (RuntimeException) e;%0A%09%09%09%7D else %7B%0A%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A
@@ -1508,17 +1508,20 @@
 tRoot(),
- 
+%0A%09%09%09
 PitchCla

Modify the pitch class namer to set the names via constructor and throw a runtime exception instead of just silently continuing.