View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25   *
26   */
27  package com.drazzib.netpisteur.monitor;
28  
29  import java.io.BufferedWriter;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.io.WriteAbortedException;
34  import java.util.ArrayList;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * Represents the class ListeClick.java which is used in order to collect every
41   * click done by the netsurfer to know where, when on the page this character
42   * has clicked.
43   * 
44   * @author NetPisteur Team
45   */
46  public class ListClick {
47  	
48  	private static Log log = LogFactory.getLog(ListClick.class);
49  
50  	// attributes
51  
52  	/**
53  	 * Represents the clicks'number during a passage on one page and a type's
54  	 * array Vector in order to collect all the clicks.
55  	 * 
56  	 */
57  	// click's page number
58  	private int nbClickPage;
59  
60  	// list of click's Vector
61  	private ArrayList _listClicks;
62  
63  	/**
64  	 * empty constructor which initializes the click's page at zero & create a
65  	 * null Vector
66  	 * 
67  	 * 
68  	 */
69  	public ListClick() {
70  		this.nbClickPage = 0;
71  		this._listClicks = new ArrayList();
72  	} // ListeClick's end
73  
74  	/**
75  	 * normal constructor which initializes the click's page & the Vector those
76  	 * passed in parameter in the constructor
77  	 * 
78  	 * @param _nbClickPage
79  	 *            click's page number
80  	 * @param uneListeClicks
81  	 *            a click's list Vector
82  	 * 
83  	 */
84  	public ListClick(int _nbClickPage, ArrayList uneListeClicks) {
85  		this.nbClickPage = _nbClickPage;
86  		this._listClicks = uneListeClicks;
87  	} // ListeClick's end
88  
89  	// access methods for attributes
90  
91  	/**
92  	 * get the number of Clicks in return of an object of Click's type
93  	 * 
94  	 * @return Click's type object
95  	 * @param index :
96  	 *            the index where search the click in the list
97  	 */
98  	public Click getClick(int index) {
99  		Click click = null;
100 		if (this._listClicks.size() == 0) {
101 			throw new RuntimeException("Aucun click d'enregistr !");
102 		}
103 		if (index > this._listClicks.size()) {
104 			throw new RuntimeException();
105 		}
106 		if (index <= this._listClicks.size()) {
107 			click = (Click) this._listClicks.get(index);
108 		}
109 		return (click);
110 	} // getClick's end
111 
112 	/**
113 	 * add the object Clic to the end of the array Vector named ListeClics
114 	 * 
115 	 * @param click :
116 	 *            type's Click object
117 	 * 
118 	 */
119 	public void ajouter(Click click) {
120 		this._listClicks.add(click);
121 		this.nbClickPage++;
122 
123 	} // ajouter's end
124 
125 	/**
126 	 * write a ListClick's array
127 	 * 
128 	 * @return true or false according to the write's result
129 	 * @param file :
130 	 *            the file where record measures
131 	 * @param sep :
132 	 *            the separator of the measure
133 	 */
134 	public boolean writeListeClickToDisk(String file, String sep) {
135 		int abs, ord;
136 		Click click;
137 		PrintWriter out;
138 		boolean retour = false;
139 
140 		try {
141 			out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
142 
143 			out.println("# Liste des positions lors des clics : ");
144 			for (int i = 0; i < this._listClicks.size(); i++) {
145 				click = (Click) this._listClicks.get(i);
146 				abs = click.getAbs();
147 				ord = click.getOrd();
148 				if (i == 0) {
149 					out.println(abs + "x" + ord + sep + click.getHours() + ":"
150 							+ click.getMinutes() + ":" + click.getSeconds());
151 				} else {
152 					out.println(abs + "x" + ord + sep + click.getHours() + ":"
153 							+ click.getMinutes() + ":" + click.getSeconds()
154 							+ sep + getIntervalClick(i));
155 				}
156 			} // -for's end
157 
158 			out.println("# Nombre de clics : " + this.nbClickPage
159 					+ "  -->  intervalle moyen : " + getIntervalMoyen());
160 			out.close();
161 			retour = true;
162 
163 		} catch (WriteAbortedException e) {
164 			log.error("Erreur d'écriture du fichier", e);
165 		}
166 
167 		catch (IOException e) {
168 			log.error("Erreur physique de lecture du fichier", e);
169 		}
170 		return (retour);
171 
172 	} // writeListToDisk's end
173 
174 	/*
175 	 * give the middle time's interval between the first & the last click
176 	 * 
177 	 * @return the middle interval
178 	 * 
179 	 */
180 
181 	public long getIntervalMoyen() {
182 		long interval = 0;
183 
184 		if (this._listClicks.size() > 0) {
185 			// first click
186 			Click premClic = (Click) this._listClicks.get(0);
187 
188 			// last click
189 			Click derClic = (Click) this._listClicks.get(this.nbClickPage - 1);
190 
191 			long time1 = premClic.getMilliSec();
192 			long time2 = derClic.getMilliSec();
193 
194 			interval = (time2 - time1) / this.nbClickPage - 1;
195 		}
196 		return interval;
197 	} // getIntervalMoyen's end
198 
199 	/*
200 	 * give the time's interval between every click around the click put in
201 	 * parameter
202 	 * 
203 	 * @return the interval before and after the click
204 	 * 
205 	 */
206 
207 	public String getIntervalClick(int index) {
208 		String inter = "";
209 
210 		if (this._listClicks.size() > 0) {
211 
212 			if ((index > 0) && (index < this.nbClickPage - 1)) {
213 				Click clic = (Click) this._listClicks.get(index);
214 				Click aclic = (Click) this._listClicks.get(index - 1);
215 				Click pclic = (Click) this._listClicks.get(index + 1);
216 
217 				long time = clic.getMilliSec();
218 				long atime = aclic.getMilliSec();
219 				long ptime = pclic.getMilliSec();
220 
221 				long avantInter = (time - atime);
222 				long apresInter = (ptime - time);
223 
224 				inter = "intervalle pour " + (index + 1)
225 						+ "ime clic : avant (" + (avantInter) + ") / aprs ("
226 						+ (apresInter) + ")";
227 
228 			} // if's end
229 
230 			else {
231 				if (index == this.nbClickPage - 1) {
232 					Click clic = (Click) this._listClicks.get(index);
233 					Click aclic = (Click) this._listClicks.get(index - 1);
234 
235 					long time = clic.getMilliSec();
236 					long atime = aclic.getMilliSec();
237 
238 					long avantInter = time - atime;
239 
240 					inter = "intervalle pour " + (index + 1)
241 							+ "ime clic : avant (" + (avantInter)
242 							+ ") / aprs (--)";
243 				}
244 			} // else's end
245 		} // else liste vide
246 
247 		return (inter);
248 
249 	} // getIntervalClick's end
250 
251 } // ListeClick class'end
252