1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ui.history;
15 
16 import java.util.ArrayList;
17 import java.util.Calendar;
18 
19 import org.eclipse.team.core.history.IFileRevision;
20 
21 public class DateHistoryCategory extends AbstractHistoryCategory {
22 	private String name;
23 	private Calendar fromDate;
24 	private Calendar toDate;
25 	private IFileRevision[] revisions;
26 
27 	/**
28 	 * Creates a new instance of DateCVSHistoryCategory.
29 	 *
30 	 * @param name	the name of this category
31 	 * @param fromDate	the start date for this category or <code>null</code> if you want everything up to the end date
32 	 * @param toDate	the end point for this category or <code>null</code> if you want just all entries in the
33 	 * start date
34 	 */
DateHistoryCategory(String name, Calendar fromDate, Calendar toDate)35 	public DateHistoryCategory(String name, Calendar fromDate, Calendar toDate){
36 		this.name = name;
37 		this.fromDate = fromDate;
38 		this.toDate = toDate;
39 	}
40 
41 	@Override
getName()42 	public String getName() {
43 		return name;
44 	}
45 
46 	@Override
collectFileRevisions(IFileRevision[] fileRevisions, boolean shouldRemove)47 	public boolean collectFileRevisions(IFileRevision[] fileRevisions, boolean shouldRemove) {
48 		ArrayList<IFileRevision> pertinentRevisions = new ArrayList<>();
49 		ArrayList<IFileRevision> nonPertinentRevisions = new ArrayList<>();
50 
51 		for (IFileRevision fileRevision : fileRevisions) {
52 			//get the current file revision's date
53 			Calendar fileRevDate = Calendar.getInstance();
54 			fileRevDate.setTimeInMillis(fileRevision.getTimestamp());
55 			int fileRevDay = fileRevDate.get(Calendar.DAY_OF_YEAR);
56 			int fileRevYear = fileRevDate.get(Calendar.YEAR);
57 			if (fromDate == null) {
58 				//check to see if this revision is within the toDate range
59 				if (((fileRevDay<toDate.get(Calendar.DAY_OF_YEAR)) && (fileRevYear == toDate.get(Calendar.YEAR))) ||
60 						(fileRevYear < toDate.get(Calendar.YEAR))) {
61 					pertinentRevisions.add(fileRevision);
62 				} else {
63 					//revision is equal or later then the to date, add to rejects list
64 					nonPertinentRevisions.add(fileRevision);
65 				}
66 			} else if (toDate == null) {
67 				//check to see if this revision falls on the same day as the fromDate
68 				if ((fileRevDay == fromDate.get(Calendar.DAY_OF_YEAR)) &&
69 						(fileRevYear == fromDate.get(Calendar.YEAR))) {
70 					pertinentRevisions.add(fileRevision);
71 				} else {
72 					nonPertinentRevisions.add(fileRevision);
73 				}
74 			} else {
75 				//check the range
76 				if ((fileRevYear >= fromDate.get(Calendar.YEAR)) &&
77 						(fileRevYear <= toDate.get(Calendar.YEAR)) &&
78 						(fileRevDay >= fromDate.get(Calendar.DAY_OF_YEAR)) &&
79 						(fileRevDay < toDate.get(Calendar.DAY_OF_YEAR))) {
80 					pertinentRevisions.add(fileRevision);
81 				} else {
82 					nonPertinentRevisions.add(fileRevision);
83 				}
84 			}
85 		}
86 
87 		//check mode
88 		if (shouldRemove){
89 			//TODO: pass in an object containing the file revision arrays and modify the contents
90 			/*IFileRevision[] tempRevision = (IFileRevision[]) nonPertinentRevisions.toArray(new IFileRevision[nonPertinentRevisions.size()]);
91 			System.arraycopy(tempRevision, 0, fileRevisions, 0, tempRevision.length);*/
92 		}
93 
94 		if (pertinentRevisions.size() > 0){
95 			IFileRevision[] tempRevision = pertinentRevisions.toArray(new IFileRevision[pertinentRevisions.size()]);
96 			revisions = new IFileRevision[tempRevision.length];
97 			System.arraycopy(tempRevision, 0, revisions, 0, tempRevision.length);
98 			return true;
99 		}
100 
101 		return false;
102 	}
103 
104 	@Override
getRevisions()105 	public IFileRevision[] getRevisions() {
106 		return revisions;
107 	}
108 
109 	@Override
hasRevisions()110 	public boolean hasRevisions() {
111 		return revisions != null && revisions.length != 0;
112 	}
113 }
114