1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.modules.process; 22 23 import java.util.ArrayList; 24 import java.util.Collection; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 import net.sourceforge.atunes.model.ILocalAudioObject; 30 import net.sourceforge.atunes.utils.StringUtils; 31 32 /** 33 * Process to remove common prefix from a list of files 34 * 35 * @author alex 36 * 37 */ 38 public class RemoveCommonPrefixProcess extends AbstractChangeTagProcess { 39 40 /** The files and titles. */ 41 private Map<ILocalAudioObject, String> filesAndTitles; 42 43 @Override retrieveInformationBeforeChangeTags()44 protected void retrieveInformationBeforeChangeTags() { 45 if (this.filesAndTitles == null) { 46 this.filesAndTitles = getTitlesForFiles(getFilesToChange()); 47 } 48 } 49 50 @Override changeTag(final ILocalAudioObject file)51 protected void changeTag(final ILocalAudioObject file) { 52 String newTitle = this.filesAndTitles.get(file); 53 getTagHandler().setTitle(file, newTitle); 54 } 55 56 /** 57 * @param filesAndTitles 58 * the filesAndTitles to set 59 */ setFilesAndTitles( final Map<ILocalAudioObject, String> filesAndTitles)60 public void setFilesAndTitles( 61 final Map<ILocalAudioObject, String> filesAndTitles) { 62 this.filesAndTitles = filesAndTitles; 63 } 64 65 /** 66 * Returns a hash of files with its songs titles. 67 * 68 * @param files 69 * the files 70 * 71 * @return the titles for files 72 */ getTitlesForFiles( final Collection<ILocalAudioObject> files)73 private Map<ILocalAudioObject, String> getTitlesForFiles( 74 final Collection<ILocalAudioObject> files) { 75 76 // First get the common prefix 77 List<String> titles = new ArrayList<String>(); 78 for (ILocalAudioObject file : files) { 79 titles.add(file.getTitleOrFileName()); 80 } 81 String prefix = StringUtils.getCommonPrefix(titles 82 .toArray(new String[titles.size()])); 83 84 Map<ILocalAudioObject, String> result = new HashMap<ILocalAudioObject, String>(); 85 // For each file create the new title without prefix 86 for (ILocalAudioObject f : files) { 87 String title = org.apache.commons.lang.StringUtils.removeStart( 88 f.getTitleOrFileName(), prefix); 89 if (title != null) { 90 result.put(f, title); 91 } 92 } 93 94 // Return files matched 95 return result; 96 } 97 } 98