annotate src/org/tmatesoft/hg/internal/FilterByteChannel.java @ 653:629a7370554c

Tests for recent changes in HgParentChildMap and RepositoryComparator (outgoing to respect drafts and Issue 47)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 03 Jul 2013 14:38:30 +0200
parents 91d75e1bac9f
children
rev   line source
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
130
7567f4a42fe5 Correct contact address
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 115
diff changeset
15 * contact TMate Software at support@hg4j.com
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
148
1a7a9a20e1f9 Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 130
diff changeset
19 import java.io.IOException;
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.nio.ByteBuffer;
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
21 import java.util.ArrayList;
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.Collection;
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
23 import java.util.List;
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 148
diff changeset
25 import org.tmatesoft.hg.util.Adaptable;
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.util.ByteChannel;
148
1a7a9a20e1f9 Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 130
diff changeset
27 import org.tmatesoft.hg.util.CancelledException;
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 /**
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 *
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @author Artem Tikhomirov
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author TMate Software Ltd.
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 */
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 148
diff changeset
34 public class FilterByteChannel implements ByteChannel, Adaptable {
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 private final Filter[] filters;
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 private final ByteChannel delegate;
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public FilterByteChannel(ByteChannel delegateChannel, Collection<Filter> filtersToApply) {
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 if (delegateChannel == null || filtersToApply == null) {
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 throw new IllegalArgumentException();
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 }
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 delegate = delegateChannel;
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 filters = filtersToApply.toArray(new Filter[filtersToApply.size()]);
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 }
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45
148
1a7a9a20e1f9 Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 130
diff changeset
46 public int write(ByteBuffer buffer) throws IOException, CancelledException {
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 final int srcPos = buffer.position();
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 ByteBuffer processed = buffer;
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 for (Filter f : filters) {
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 // each next filter consumes not more than previous
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 // hence total consumed equals position shift in the original buffer
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 processed = f.filter(processed);
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 delegate.write(processed);
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 return buffer.position() - srcPos; // consumed as much from original buffer
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 148
diff changeset
58 // adapters or implemented interfaces of the original class shall not be obfuscated by filter
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 148
diff changeset
59 public <T> T getAdapter(Class<T> adapterClass) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
60 if (adapterClass == Preview.class) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
61 ArrayList<Preview> previewers = new ArrayList<Preview>(filters.length);
356
91d75e1bac9f Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 355
diff changeset
62 Adaptable.Factory<Preview> factory = new Adaptable.Factory<Preview>(Preview.class);
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
63 for (Filter f : filters) {
356
91d75e1bac9f Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 355
diff changeset
64 Preview p = factory.get(f);
91d75e1bac9f Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 355
diff changeset
65 if (p != null) {
91d75e1bac9f Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 355
diff changeset
66 previewers.add(p);
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
67 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
68 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
69 if (!previewers.isEmpty()) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
70 @SuppressWarnings("unchecked")
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
71 T rv = (T) new PreviewSupport(previewers);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
72 return rv;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
73 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
74 // fall through to let delegate answer
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
75 }
356
91d75e1bac9f Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 355
diff changeset
76 return Adaptable.Factory.getAdapter(delegate, adapterClass, null);
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 148
diff changeset
77 }
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
78
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
79 private static class PreviewSupport implements Preview {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
80 private final Preview[] participants;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
81
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
82 public PreviewSupport(List<Preview> previewers) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
83 participants = new Preview[previewers.size()];
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
84 previewers.toArray(participants);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
85 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
86
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
87 public void preview(ByteBuffer src) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
88 final int originalPos = src.position();
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
89 for (Preview p : participants) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
90 p.preview(src);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
91 // reset to initial state
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
92 src.position(originalPos);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
93 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
94 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
95 }
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }