001    /////////////////////////////////////////////////
002    //This file is part of Sears project.
003    //Subtitle Editor And Re-Synch
004    //A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    //at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    /////////////////////////////////////////////////
017    
018    // some suggestions about this class: floriaen@gmail.com
019    
020    package sears.search.data;
021    
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    
025    import sears.file.Subtitle;
026    
027    /**
028     * Search operations 
029     *
030     */
031    public class SubtitleFile {
032            
033            private ArrayList<Subtitle> listOfSubtitles;
034            
035            /**
036             * 
037             * @param aListOfSubtitles
038             */
039            public SubtitleFile(ArrayList<Subtitle> aListOfSubtitles) {
040                    setListOfSubtitles(aListOfSubtitles);
041            }
042            
043            /**
044             * 
045             * @param aListOfSubtitles
046             */
047            public void setListOfSubtitles(ArrayList<Subtitle> aListOfSubtitles) {
048                    if( aListOfSubtitles == null ) {
049                            // ensures that the list is not null
050                            // seek methods could not performs on a null array
051                            throw new NullPointerException("the list of subtitle cannot be null here");
052                    }
053                    listOfSubtitles = aListOfSubtitles;
054            }
055            
056            /**
057             * Gets the count of all the text occurrences given in parameters
058             * @param text  a non null and empty string 
059             * @return              the occurrences count
060             * @throws              NullPointerException if <tt>text</tt> is null
061             * @throws              IllegalArgumentException if <tt>text</tt> is an empty string
062             */
063            public int getCountOfOccurrencesOfText(String text) {
064                    if( text == null ) {
065                            throw new NullPointerException("cannot search a null object");
066                    }
067                    if( text.trim().length() == 0 ) {
068                            throw new IllegalArgumentException("cannot search an empty string");
069                    }
070                    int count = 0;
071                    text = text.toLowerCase();              
072                    Subtitle subtitle = null;
073                    String str = "";
074                    Iterator<Subtitle> it = listOfSubtitles.iterator();
075                    while( it.hasNext() ) {
076                            subtitle = (Subtitle) it.next();                        
077                            str = subtitle.getSubtitle().toLowerCase();
078                            int index = str.indexOf(text);
079                            while( index != -1 ) {
080                                    count++;
081                                    index = str.indexOf(text, index + 1);
082                            }
083                    }
084                    return count;
085            }
086            
087            /**
088             * Constructs and return a list of all row that contains at least one occurrence of <tt>text</tt>
089             * @param text  a non null and a non empty string string
090             * @return              the list of all the row that contains at least one occurrence of <tt>text</tt>
091             */
092            public ListOfRow getAllRowWhichContainsAtLeastOneOccurrenceOfText(String text) {
093                    if( text == null ) {
094                            throw new NullPointerException("cannot search a null object");
095                    }
096                    if( text.trim().length() == 0 ) {
097                            throw new IllegalArgumentException("cannot search an empty string");
098                    }
099                    // !! SIZE STATIC METHOD -->
100                    int size = 100;
101                    // <--
102                    // index of row in the list of subtitles:
103                    int index = -1;
104                    ListOfRow listOfRow = new ListOfRow(size);
105                    text = text.toLowerCase();              
106                    Subtitle subtitle = null;
107                    String str = "";
108                    Iterator<Subtitle> it = listOfSubtitles.iterator();
109                    while( it.hasNext() ) {
110                            index++;
111                            subtitle = (Subtitle) it.next();                        
112                            str = subtitle.getSubtitle().toLowerCase();
113                            int result = str.indexOf(text);
114                            if( result != -1 ) {
115                                    listOfRow.add(index);
116                            }
117                    }
118                    return listOfRow;
119            }
120    }