Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/NewlineFilter.java @ 356:91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 06 Dec 2011 14:25:52 +0100 |
parents | f2c11fe7f3e9 |
children | 9c9c442b5f2e |
comparison
equal
deleted
inserted
replaced
355:f2c11fe7f3e9 | 356:91d75e1bac9f |
---|---|
29 import java.util.Map; | 29 import java.util.Map; |
30 | 30 |
31 import org.tmatesoft.hg.core.HgBadStateException; | 31 import org.tmatesoft.hg.core.HgBadStateException; |
32 import org.tmatesoft.hg.repo.HgInternals; | 32 import org.tmatesoft.hg.repo.HgInternals; |
33 import org.tmatesoft.hg.repo.HgRepository; | 33 import org.tmatesoft.hg.repo.HgRepository; |
34 import org.tmatesoft.hg.util.Adaptable; | |
34 import org.tmatesoft.hg.util.Path; | 35 import org.tmatesoft.hg.util.Path; |
35 | 36 |
36 /** | 37 /** |
37 * | 38 * |
38 * @author Artem Tikhomirov | 39 * @author Artem Tikhomirov |
39 * @author TMate Software Ltd. | 40 * @author TMate Software Ltd. |
40 */ | 41 */ |
41 public class NewlineFilter implements Filter, Preview { | 42 public class NewlineFilter implements Filter, Preview, Adaptable { |
42 | 43 |
43 // if processInconsistent is false, filter simply pass incorrect newline characters (single \r or \r\n on *nix and single \n on Windows) as is, | 44 // if processInconsistent is false, filter simply pass incorrect newline characters (single \r or \r\n on *nix and single \n on Windows) as is, |
44 // i.e. doesn't try to convert them into appropriate newline characters. | 45 // i.e. doesn't try to convert them into appropriate newline characters. |
45 // XXX revisit if Keyword extension behaves differently - WTF??? | 46 // XXX revisit if Keyword extension behaves differently - WTF??? |
46 private final boolean processInconsistent; | 47 private final boolean processInconsistent; |
63 winToNix = transform == 0; | 64 winToNix = transform == 0; |
64 processInconsistent = !onlyConsistent; | 65 processInconsistent = !onlyConsistent; |
65 } | 66 } |
66 | 67 |
67 public ByteBuffer filter(ByteBuffer src) { | 68 public ByteBuffer filter(ByteBuffer src) { |
68 if (!previewDone) { | 69 if (!processInconsistent && !previewDone) { |
69 throw new HgBadStateException("This filter requires preview operation prior to actual filtering"); | 70 throw new HgBadStateException("This filter requires preview operation prior to actual filtering when eol.only-consistent is true"); |
70 } | 71 } |
71 if (!processInconsistent && foundLoneLF && foundCRLF) { | 72 if (!processInconsistent && foundLoneLF && foundCRLF) { |
72 // do not process inconsistent newlines | 73 // do not process inconsistent newlines |
73 return src; | 74 return src; |
74 } | 75 } |
84 } | 85 } |
85 return nix2win(src); | 86 return nix2win(src); |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
90 public <T> T getAdapter(Class<T> adapterClass) { | |
91 // conditionally through getAdapter | |
92 if (Preview.class == adapterClass) { | |
93 // when processInconsistent is false, we need to preview data stream to ensure line terminators are consistent. | |
94 // otherwise, no need to look into the stream | |
95 if (!processInconsistent) { | |
96 return adapterClass.cast(this); | |
97 } | |
98 } | |
99 return null; | |
100 } | |
101 | |
89 private boolean prevBufLastByteWasCR = false; | 102 private boolean prevBufLastByteWasCR = false; |
90 private boolean previewDone = false; | 103 private boolean previewDone = false; |
91 | 104 |
92 public void preview(ByteBuffer src) { | 105 public void preview(ByteBuffer src) { |
93 previewDone = true; // guard | 106 previewDone = true; // guard |
94 if (processInconsistent) { | 107 if (processInconsistent) { |
95 // gonna handle them anyway, no need to check. TODO Do not implement Preview directly, but rather | 108 // gonna handle them anyway, no need to check. TODO Do not implement Preview directly, but rather |
96 // conditionally through getAdapter when processInconsistent is false (sic!) | |
97 return; | 109 return; |
98 } | 110 } |
99 if (foundLoneLF && foundCRLF) { | 111 if (foundLoneLF && foundCRLF) { |
100 // already know it's inconsistent | 112 // already know it's inconsistent |
101 return; | 113 return; |