<commit_before>/*******************************************************************************
 * Copyright 2013 <NAME>, <NAME>, Public Radio Exchange
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.prx.playerhater.songs;

import java.util.HashMap;
import java.util.Map;

import org.prx.playerhater.Song;
import org.prx.playerhater.ipc.IPlayerHaterClient;
import org.prx.playerhater.ipc.IPlayerHaterServer;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.SparseArray;

public class SongHost {

	public static final int INVALID_TAG = -1;

	private static Remote sRemote;
	private static SparseArray<Song> sSongs;
	private static Map<Song, Integer> sTags;

	public static void setRemote(Remote remote) {
		sRemote = remote;
	}

	public static void setRemote(IPlayerHaterClient client) {
		sRemote = new ClientRemote(client);
	}

	public static void setRemote(IPlayerHaterServer server) {
		sRemote = new ServerRemote(server);
	}

	public static void slurp(int songTag, Bundle songData) {
		Song song = getSong(songTag);
		if (song instanceof RemoteSong) {
			((RemoteSong) getSong(songTag)).setSong(Songs.fromBundle(songData));
		}
	}

	public static SparseArray<Bundle> localSongs() {
		SparseArray<Bundle> data = new SparseArray<Bundle>();
		for (Song song : getTags().keySet()) {
			if (!(song instanceof RemoteSong)) {
				data.put(getTag(song), Songs.toBundle(song));
			}
		}
		return data;
	}

	static Remote remote() {
		if (sRemote == null) { 
			return new NullRemote(); 
		}
		return sRemote;
	}

	public static void clear() {
		sRemote = null;
		sSongs = null;
		sTags = null;
	}

	public static int getTag(Song song) {
		if (song == null) {
			return INVALID_TAG;
		}
		if (getTags().containsKey(song)) {
			return getTags().get(song);
		} else {
			int tag = song.hashCode();
			getTags().put(song, tag);
			getSongs().put(tag, song);
			return tag;
		}
	}

	public static Song getSong(int tag) {
		if (tag == INVALID_TAG) {
			return null;
		}
		Song song = getSongs().get(tag);
		if (song != null) {
			return song;
		} else {
			song = new RemoteSong(tag);
			getTags().put(song, tag);
			getSongs().put(tag, song);
			return song;
		}
	}
	
	public static Song getSong(int tag, Bundle songData) { 
		Song song = getSong(tag); 
		if (song instanceof RemoteSong) { 
			((RemoteSong) song).setSong(Songs.fromBundle(songData));
		}
		return song; 
	}
	
	public static Song getLocalSong(int tag) { 
		Song song = getSongs().get(tag); 
		if (song instanceof RemoteSong) { 
			return ((RemoteSong) song).getSong(); 
		}
		return song; 
	}

	private static SparseArray<Song> getSongs() {
		if (sSongs == null) {
			sSongs = new SparseArray<Song>();
		}
		return sSongs;
	}

	private static Map<Song, Integer> getTags() {
		if (sTags == null) {
			sTags = new HashMap<Song, Integer>();
		}
		return sTags;
	}

	static interface Remote {
		Uri getSongAlbumArt(int tag) throws RemoteException;

		Uri getSongUri(int tag) throws RemoteException;

		String getSongAlbumTitle(int tag) throws RemoteException;

		String getSongTitle(int tag) throws RemoteException;

		String getSongArtist(int tag) throws RemoteException;<commit_msg>making getLocalSong throw an exception if it fails to find a Song or a serialized Song stored in a Remote Song<commit_after>/*******************************************************************************
 * Copyright 2013 <NAME>, <NAME>, Public Radio Exchange
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.prx.playerhater.songs;

import java.util.HashMap;
import java.util.Map;

import org.prx.playerhater.Song;
import org.prx.playerhater.ipc.IPlayerHaterClient;
import org.prx.playerhater.ipc.IPlayerHaterServer;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.SparseArray;

public class SongHost {

	public static final int INVALID_TAG = -1;

	private static Remote sRemote;
	private static SparseArray<Song> sSongs;
	private static Map<Song, Integer> sTags;

	public static void setRemote(Remote remote) {
		sRemote = remote;
	}

	public static void setRemote(IPlayerHaterClient client) {
		sRemote = new ClientRemote(client);
	}

	public static void setRemote(IPlayerHaterServer server) {
		sRemote = new ServerRemote(server);
	}

	public static void slurp(int songTag, Bundle songData) {
		Song song = getSong(songTag);
		if (song instanceof RemoteSong) {
			((RemoteSong) getSong(songTag)).setSong(Songs.fromBundle(songData));
		}
	}

	public static SparseArray<Bundle> localSongs() {
		SparseArray<Bundle> data = new SparseArray<Bundle>();
		for (Song song : getTags().keySet()) {
			if (!(song instanceof RemoteSong)) {
				data.put(getTag(song), Songs.toBundle(song));
			}
		}
		return data;
	}

	static Remote remote() {
		if (sRemote == null) { 
			return new NullRemote(); 
		}
		return sRemote;
	}

	public static void clear() {
		sRemote = null;
		sSongs = null;
		sTags = null;
	}

	public static int getTag(Song song) {
		if (song == null) {
			return INVALID_TAG;
		}
		if (getTags().containsKey(song)) {
			return getTags().get(song);
		} else {
			int tag = song.hashCode();
			getTags().put(song, tag);
			getSongs().put(tag, song);
			return tag;
		}
	}

	public static Song getSong(int tag) {
		if (tag == INVALID_TAG) {
			return null;
		}
		Song song = getSongs().get(tag);
		if (song != null) {
			return song;
		} else {
			song = new RemoteSong(tag);
			getTags().put(song, tag);
			getSongs().put(tag, song);
			return song;
		}
	}
	
	public static Song getSong(int tag, Bundle songData) { 
		Song song = getSong(tag); 
		if (song instanceof RemoteSong) { 
			((RemoteSong) song).setSong(Songs.fromBundle(songData));
		}
		return song; 
	}
	
	public static Song getLocalSong(int tag) { 
		Song song = getSongs().get(tag); 
		if (song instanceof RemoteSong) { 
			song = ((RemoteSong) song).getSong(); 
		}
		if (song == null) { 
			throw new IllegalStateException("No locally stored song data available."); 
		}
		return song; 
	}

	private static SparseArray<Song> getSongs() {
		if (sSongs == null) {
			sSongs = new SparseArray<Song>();
		}
		return sSongs;
	}

	private static Map<Song, Integer> getTags() {
		if (sTags == null) {
			sTags = new HashMap<Song, Integer>();
		}
		return sTags;
	}

	static interface Remote {
		Uri getSongAlbumArt(int tag) throws RemoteException;

		Uri getSongUri(int tag) throws RemoteException;

		String getSongAlbumTitle(int tag) throws RemoteException;

		String getSongTitle(int tag) throws RemoteException;

		String getSongArtist(int tag) throws RemoteException;
{'fraction_non_alphanumeric': 0.1468890314304041, 'fraction_numerical': 0.002309172546504169, 'mean_word_length': 4.102094240837697, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482956', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2352, 'n_words': 728}