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 /**
57 * Selects commits based upon the commit time field.
58 */
59 public abstract class CommitTimeRevFilter extends RevFilter {
60 /**
61 * Create a new filter to select commits before a given date/time.
62 *
63 * @param ts
64 * the point in time to cut on.
65 * @return a new filter to select commits on or before <code>ts</code>.
66 */
67 public static final RevFilter before(Date ts) {
68 return before(ts.getTime());
69 }
70
71 /**
72 * Create a new filter to select commits before a given date/time.
73 *
74 * @param ts
75 * the point in time to cut on, in milliseconds
76 * @return a new filter to select commits on or before <code>ts</code>.
77 */
78 public static final RevFilter before(long ts) {
79 return new Before(ts);
80 }
81
82 /**
83 * Create a new filter to select commits after a given date/time.
84 *
85 * @param ts
86 * the point in time to cut on.
87 * @return a new filter to select commits on or after <code>ts</code>.
88 */
89 public static final RevFilter after(Date ts) {
90 return after(ts.getTime());
91 }
92
93 /**
94 * Create a new filter to select commits after a given date/time.
95 *
96 * @param ts
97 * the point in time to cut on, in milliseconds.
98 * @return a new filter to select commits on or after <code>ts</code>.
99 */
100 public static final RevFilter after(long ts) {
101 return new After(ts);
102 }
103
104 /**
105 * Create a new filter to select commits after or equal a given date/time <code>since</code>
106 * and before or equal a given date/time <code>until</code>.
107 *
108 * @param since the point in time to cut on.
109 * @param until the point in time to cut off.
110 * @return a new filter to select commits between the given date/times.
111 */
112 public static final RevFilter between(Date since, Date until) {
113 return between(since.getTime(), until.getTime());
114 }
115
116 /**
117 * Create a new filter to select commits after or equal a given date/time <code>since</code>
118 * and before or equal a given date/time <code>until</code>.
119 *
120 * @param since the point in time to cut on, in milliseconds.
121 * @param until the point in time to cut off, in millisconds.
122 * @return a new filter to select commits between the given date/times.
123 */
124 public static final RevFilter between(long since, long until) {
125 return new Between(since, until);
126 }
127
128 final int when;
129
130 CommitTimeRevFilter(long ts) {
131 when = (int) (ts / 1000);
132 }
133
134 /** {@inheritDoc} */
135 @Override
136 public RevFilter clone() {
137 return this;
138 }
139
140 /** {@inheritDoc} */
141 @Override
142 public boolean requiresCommitBody() {
143 return false;
144 }
145
146 private static class Before extends CommitTimeRevFilter {
147 Before(long ts) {
148 super(ts);
149 }
150
151 @Override
152 public boolean include(RevWalk walker, RevCommit cmit)
153 throws StopWalkException, MissingObjectException,
154 IncorrectObjectTypeException, IOException {
155 return cmit.getCommitTime() <= when;
156 }
157
158 @SuppressWarnings("nls")
159 @Override
160 public String toString() {
161 return super.toString() + "(" + new Date(when * 1000L) + ")";
162 }
163 }
164
165 private static class After extends CommitTimeRevFilter {
166 After(long ts) {
167 super(ts);
168 }
169
170 @Override
171 public boolean include(RevWalk walker, RevCommit cmit)
172 throws StopWalkException, MissingObjectException,
173 IncorrectObjectTypeException, IOException {
174 // Since the walker sorts commits by commit time we can be
175 // reasonably certain there is nothing remaining worth our
176 // scanning if this commit is before the point in question.
177 //
178 if (cmit.getCommitTime() < when)
179 throw StopWalkException.INSTANCE;
180 return true;
181 }
182
183 @SuppressWarnings("nls")
184 @Override
185 public String toString() {
186 return super.toString() + "(" + new Date(when * 1000L) + ")";
187 }
188 }
189
190 private static class Between extends CommitTimeRevFilter {
191 private final int until;
192
193 Between(long since, long until) {
194 super(since);
195 this.until = (int) (until / 1000);
196 }
197
198 @Override
199 public boolean include(RevWalk walker, RevCommit cmit)
200 throws StopWalkException, MissingObjectException,
201 IncorrectObjectTypeException, IOException {
202 return cmit.getCommitTime() <= until && cmit.getCommitTime() >= when;
203 }
204
205 @SuppressWarnings("nls")
206 @Override
207 public String toString() {
208 return super.toString() + "(" + new Date(when * 1000L) + " - "
209 + new Date(until * 1000L) + ")";
210 }
211
212 }
213
214 }