package itunes; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import com.dd.plist.NSArray; import com.dd.plist.NSDictionary; import com.dd.plist.NSObject; import com.dd.plist.PropertyListParser; /** * An iTunes library. * * @author Franck van Breugel */ public class Library implements Iterable { private List playLists; private Map tracks; /** * Initializes this library from the file with the given name. * * @param fileName name of the file. * @throws Exception if parsing of the file with the given name failed. */ public Library(String fileName) throws Exception { File file = new File(fileName); NSDictionary dictionary = (NSDictionary) PropertyListParser.parse(file); Map libraryMap = dictionary.getHashMap(); NSObject[] playListsArray = ((NSArray) libraryMap.get("Playlists")).getArray(); List playLists = new ArrayList(); for (NSObject object : playListsArray) { playLists.add(new PlayList((NSDictionary) object)); } this.setPlayLists(playLists); Map tracksMap = ((NSDictionary) libraryMap.get("Tracks")).getHashMap(); Map tracks = new HashMap(); for (String key : tracksMap.keySet()) { tracks.put(Integer.parseInt(key), new Track((NSDictionary) tracksMap.get(key))); } this.setTracks(tracks); } /** * Returns the play lists of this library. * * @return the play lists of this library. */ private List getPlayLists() { return this.playLists; } /** * Sets the play lists of this library to the given play lists. * * @param playLists the new play lists of this library. */ private void setPlayLists(List playLists) { this.playLists = playLists; } /** * Returns the tracks of this library. * * @return the tracks of this library. */ private Map getTracks() { return this.tracks; } /** * Sets the tracks of this library to the given tracks. * * @param tracks the new tracks of this library. */ private void setTracks(Map tracks) { this.tracks = tracks; } /** * Returns the number of play lists of this library. * * @return the number of play lists of this library. */ public int numberOfPlayLists() { return this.getPlayLists().size(); } /** * Returns the play list of this library with the given index. * * @param index the index of the play list. * @pre. index >= 0 and index < numberOfPlayLists() * @return the play list of this library with the given index. */ public PlayList get(int index) { return this.getPlayLists().get(index); } /** * Returns the play list of this library with the given index. * * @param index the index of the play list. * @pre. index >= 0 and index < numberOfPlayLists() * @return the play list of this library with the given index. */ public Track getTrack(int ID) { return this.getTracks().get(ID); } /** * Returns the tracks of this library. * * @return the tracks of this library. */ public Iterator iterator() { return this.getTracks().values().iterator(); } }