Java tutorial
/** * Media Library class * * * Copyright (c) 2015 Jacob Gersztyn * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * @author Jacob Gersztyn * @version 2015-09-10 * */ import java.util.HashMap; import org.json.*; import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class MediaLibrary { private HashMap<String, MediaDescription> mediaFiles; private int count; public MediaLibrary() { this.mediaFiles = new HashMap<String, MediaDescription>(); count = 0; } public MediaLibrary(String jsonFilename) { this.mediaFiles = new HashMap<String, MediaDescription>(); count = 0; Scanner ifstr = null; try { ifstr = new Scanner(new FileInputStream(jsonFilename)); StringBuilder sb = new StringBuilder(); while (ifstr.hasNext()) { sb.append(ifstr.next()); } JSONObject obj = new JSONObject(sb.toString()); for (String title : JSONObject.getNames(obj)) { JSONObject tobj = new JSONObject(obj.get(title).toString()); MediaDescription md = new MediaDescription(tobj); mediaFiles.put(md.getTitle(), md); } } catch (FileNotFoundException fnfe) { System.out.println("File not found"); fnfe.printStackTrace(); } finally { ifstr.close(); } } public boolean add(MediaDescription aClip) { MediaDescription found = this.get(aClip.getTitle()); if (found == null) { this.mediaFiles.put(aClip.getTitle(), aClip); count++; return true; } return false; } public boolean remove(String aTitle) { MediaDescription found = this.get(aTitle); if (found != null) { this.mediaFiles.remove(aTitle); count--; return true; } return false; } public MediaDescription get(String aTitle) { return this.mediaFiles.get(aTitle); } public String[] getTitles() { int index = 0; String[] titles = new String[this.mediaFiles.size()]; for (String key : this.mediaFiles.keySet()) { titles[index] = this.mediaFiles.get(key).getTitle(); index++; } return titles; } public String[] getMusicTitles() { int index = 0; String[] titles = new String[this.mediaFiles.size()]; for (String key : this.mediaFiles.keySet()) { if (this.mediaFiles.get(key).isMusic()) { titles[index] = this.mediaFiles.get(key).getTitle(); index++; } } String[] retTitles = new String[index]; for (int i = 0; i < index; i++) retTitles[i] = titles[i]; return retTitles; } public String[] getVideoTitles() { int index = 0; String[] titles = new String[this.mediaFiles.size()]; for (String key : this.mediaFiles.keySet()) { if (!this.mediaFiles.get(key).isMusic()) { titles[index] = this.mediaFiles.get(key).getTitle(); index++; } } String[] retTitles = new String[index]; for (int i = 0; i < index; i++) retTitles[i] = titles[i]; return retTitles; } public int getCount() { return this.count; } public void toJSONFile(String jsonFilename) { PrintWriter ofstr = null; try { ofstr = new PrintWriter(new FileOutputStream(jsonFilename)); JSONObject obj = new JSONObject(); for (String title : this.mediaFiles.keySet()) { obj.put(title, this.mediaFiles.get(title).toJSONObject()); } ofstr.println(obj); } catch (FileNotFoundException fnfe) { System.out.println("File not found"); fnfe.printStackTrace(); } finally { ofstr.close(); } } }