View Javadoc
1   /*
2    * Copyright (C) 2009, Mark Struberg <struberg@yahoo.de>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.revwalk.filter;
46  
47  import java.io.IOException;
48  import java.util.Date;
49  
50  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51  import org.eclipse.jgit.errors.MissingObjectException;
52  import org.eclipse.jgit.errors.StopWalkException;
53  import org.eclipse.jgit.revwalk.RevCommit;
54  import org.eclipse.jgit.revwalk.RevWalk;
55  
56  /** Selects commits based upon the commit time field. */
57  public abstract class CommitTimeRevFilter extends RevFilter {
58  	/**
59  	 * Create a new filter to select commits before a given date/time.
60  	 *
61  	 * @param ts
62  	 *            the point in time to cut on.
63  	 * @return a new filter to select commits on or before <code>ts</code>.
64  	 */
65  	public static final RevFilter before(final Date ts) {
66  		return before(ts.getTime());
67  	}
68  
69  	/**
70  	 * Create a new filter to select commits before a given date/time.
71  	 *
72  	 * @param ts
73  	 *            the point in time to cut on, in milliseconds
74  	 * @return a new filter to select commits on or before <code>ts</code>.
75  	 */
76  	public static final RevFilter before(final long ts) {
77  		return new Before(ts);
78  	}
79  
80  	/**
81  	 * Create a new filter to select commits after a given date/time.
82  	 *
83  	 * @param ts
84  	 *            the point in time to cut on.
85  	 * @return a new filter to select commits on or after <code>ts</code>.
86  	 */
87  	public static final RevFilter after(final Date ts) {
88  		return after(ts.getTime());
89  	}
90  
91  	/**
92  	 * Create a new filter to select commits after a given date/time.
93  	 *
94  	 * @param ts
95  	 *            the point in time to cut on, in milliseconds.
96  	 * @return a new filter to select commits on or after <code>ts</code>.
97  	 */
98  	public static final RevFilter after(final long ts) {
99  		return new After(ts);
100 	}
101 
102 	/**
103 	 * Create a new filter to select commits after or equal a given date/time <code>since</code>
104 	 * and before or equal a given date/time <code>until</code>.
105 	 *
106 	 * @param since the point in time to cut on.
107 	 * @param until the point in time to cut off.
108 	 * @return a new filter to select commits between the given date/times.
109 	 */
110 	public static final RevFilter between(final Date since, final Date until) {
111 		return between(since.getTime(), until.getTime());
112 	}
113 
114 	/**
115 	 * Create a new filter to select commits after or equal a given date/time <code>since</code>
116 	 * and before or equal a given date/time <code>until</code>.
117 	 *
118 	 * @param since the point in time to cut on, in milliseconds.
119 	 * @param until the point in time to cut off, in millisconds.
120 	 * @return a new filter to select commits between the given date/times.
121 	 */
122 	public static final RevFilter between(final long since, final long until) {
123 		return new Between(since, until);
124 	}
125 
126 	final int when;
127 
128 	CommitTimeRevFilter(final long ts) {
129 		when = (int) (ts / 1000);
130 	}
131 
132 	@Override
133 	public RevFilter clone() {
134 		return this;
135 	}
136 
137 	@Override
138 	public boolean requiresCommitBody() {
139 		return false;
140 	}
141 
142 	private static class Before extends CommitTimeRevFilter {
143 		Before(final long ts) {
144 			super(ts);
145 		}
146 
147 		@Override
148 		public boolean include(final RevWalk walker, final RevCommit cmit)
149 				throws StopWalkException, MissingObjectException,
150 				IncorrectObjectTypeException, IOException {
151 			return cmit.getCommitTime() <= when;
152 		}
153 
154 		@SuppressWarnings("nls")
155 		@Override
156 		public String toString() {
157 			return super.toString() + "(" + new Date(when * 1000L) + ")";
158 		}
159 	}
160 
161 	private static class After extends CommitTimeRevFilter {
162 		After(final long ts) {
163 			super(ts);
164 		}
165 
166 		@Override
167 		public boolean include(final RevWalk walker, final RevCommit cmit)
168 				throws StopWalkException, MissingObjectException,
169 				IncorrectObjectTypeException, IOException {
170 			// Since the walker sorts commits by commit time we can be
171 			// reasonably certain there is nothing remaining worth our
172 			// scanning if this commit is before the point in question.
173 			//
174 			if (cmit.getCommitTime() < when)
175 				throw StopWalkException.INSTANCE;
176 			return true;
177 		}
178 
179 		@SuppressWarnings("nls")
180 		@Override
181 		public String toString() {
182 			return super.toString() + "(" + new Date(when * 1000L) + ")";
183 		}
184 	}
185 
186 	private static class Between extends CommitTimeRevFilter {
187 		private final int until;
188 
189 		Between(final long since, final long until) {
190 			super(since);
191 			this.until = (int) (until / 1000);
192 		}
193 
194 		@Override
195 		public boolean include(final RevWalk walker, final RevCommit cmit)
196 				throws StopWalkException, MissingObjectException,
197 				IncorrectObjectTypeException, IOException {
198 			return cmit.getCommitTime() <= until && cmit.getCommitTime() >= when;
199 		}
200 
201 		@SuppressWarnings("nls")
202 		@Override
203 		public String toString() {
204 			return super.toString() + "(" + new Date(when * 1000L) + " - "
205 					+ new Date(until * 1000L) + ")";
206 		}
207 
208 	}
209 
210 }