1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2013 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.channel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ChannelDispatcher<O> extends Channel<O> {
private static final Logger LOG = LoggerFactory.getLogger(ChannelDispatcher.class);
private final boolean failIfNoChannelToConsumeOneCharacter;
private final Channel<O>[] channels;
/**
* @deprecated in version 2.9. Please use the builder() method
*/
@SuppressWarnings("rawtypes")
@Deprecated
public ChannelDispatcher(List<Channel> channels) {
this(channels, false);
}
/**
* @deprecated in version 2.9. Please use the builder() method
*/
@SuppressWarnings("rawtypes")
@Deprecated
public ChannelDispatcher(Channel... channels) {
this(Arrays.asList(channels), false);
}
/**
* @deprecated in version 2.9. Please use the builder() method
*/
@SuppressWarnings("rawtypes")
@Deprecated
public ChannelDispatcher(List<Channel> channels, boolean failIfNoChannelToConsumeOneCharacter) {
this.channels = channels.toArray(new Channel[channels.size()]);
this.failIfNoChannelToConsumeOneCharacter = failIfNoChannelToConsumeOneCharacter;
}
private ChannelDispatcher(Builder builder) {
this.channels = builder.channels.toArray(new Channel[builder.channels.size()]);
this.failIfNoChannelToConsumeOneCharacter = builder.failIfNoChannelToConsumeOneCharacter;
}
@Override
public boolean consume(CodeReader code, O output) {
int nextChar = code.peek();
while (nextChar != -1) {
boolean characterConsumed = false;
for (Channel<O> channel : channels) {
if (channel.consume(code, output)) {
characterConsumed = true;
break;
}
}
if ( !characterConsumed) {
if (LOG.isDebugEnabled() || failIfNoChannelToConsumeOneCharacter) {
String message = "None of the channel has been able to handle character '" + (char) code.peek() + "' (decimal value "
+ code.peek() + ") at line " + code.getLinePosition() + ", column " + code.getColumnPosition();
if (failIfNoChannelToConsumeOneCharacter) {
throw new IllegalStateException(message);
}
LOG.debug(message);
}
code.pop();
}
nextChar = code.peek();
}
return true;
}
Channel[] getChannels() {
return channels;
}
/**
* Get a Builder instance to build a new ChannelDispatcher
*/
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private List<Channel> channels = new ArrayList<Channel>();
private boolean failIfNoChannelToConsumeOneCharacter = false;
private Builder() {
}
public Builder addChannel(Channel channel) {
channels.add(channel);
return this;
}
public Builder addChannels(Channel... c) {
for (Channel channel : c) {
addChannel(channel);
}
return this;
}
/**
* If this option is activated, an IllegalStateException will be thrown as soon as a character won't be consumed by any channel.
*/
public Builder failIfNoChannelToConsumeOneCharacter() {
failIfNoChannelToConsumeOneCharacter = true;
return this;
}
public <O> ChannelDispatcher<O> build() {
return new ChannelDispatcher<O>(this);
}
}
}
|