use of com.google.common.base.Splitter in project vespa by vespa-engine.
the class TemplatingTestCase method testGetNextResultURL.
@Test
public final void testGetNextResultURL() {
String next = result.getTemplating().getNextResultURL();
Set<String> expectedParameters = new HashSet<>(Arrays.asList(new String[] { "hits=5", "query=a", "presentation.format=nalle", "offset=6" }));
Set<String> actualParameters = new HashSet<>();
Splitter s = Splitter.on("&");
for (String parameter : s.split(next.substring(next.indexOf('?') + 1))) {
actualParameters.add(parameter);
}
assertEquals(expectedParameters, actualParameters);
}
use of com.google.common.base.Splitter in project vespa by vespa-engine.
the class TemplatingTestCase method testGetPreviousResultURL.
@Test
public final void testGetPreviousResultURL() {
String previous = result.getTemplating().getPreviousResultURL();
Set<String> expectedParameters = new HashSet<>(Arrays.asList(new String[] { "hits=5", "query=a", "presentation.format=nalle", "offset=0" }));
Set<String> actualParameters = new HashSet<>();
Splitter s = Splitter.on("&");
for (String parameter : s.split(previous.substring(previous.indexOf('?') + 1))) {
actualParameters.add(parameter);
}
assertEquals(expectedParameters, actualParameters);
}
use of com.google.common.base.Splitter in project cassandra by apache.
the class Client method parseLine.
private Message.Request parseLine(String line) {
Splitter splitter = Splitter.on(' ').trimResults().omitEmptyStrings();
Iterator<String> iter = splitter.split(line).iterator();
if (!iter.hasNext())
return null;
String msgType = iter.next().toUpperCase();
if (msgType.equals("STARTUP")) {
Map<String, String> options = new HashMap<String, String>();
options.put(StartupMessage.CQL_VERSION, "3.0.0");
while (iter.hasNext()) {
String next = iter.next();
if (next.toLowerCase().equals("snappy")) {
options.put(StartupMessage.COMPRESSION, "snappy");
connection.setCompressor(Compressor.SnappyCompressor.instance);
}
if (next.toLowerCase().equals("lz4")) {
options.put(StartupMessage.COMPRESSION, "lz4");
connection.setCompressor(Compressor.LZ4Compressor.instance);
}
if (next.toLowerCase().equals("throw_on_overload")) {
options.put(StartupMessage.THROW_ON_OVERLOAD, "1");
connection.setThrowOnOverload(true);
}
}
return new StartupMessage(options);
} else if (msgType.equals("QUERY")) {
line = line.substring(6);
// Ugly hack to allow setting a page size, but that's playground code anyway
String query = line;
int pageSize = -1;
if (line.matches(".+ !\\d+$")) {
int idx = line.lastIndexOf('!');
query = line.substring(0, idx - 1);
try {
pageSize = Integer.parseInt(line.substring(idx + 1, line.length()));
} catch (NumberFormatException e) {
return null;
}
}
return new QueryMessage(query, QueryOptions.create(ConsistencyLevel.ONE, Collections.<ByteBuffer>emptyList(), false, pageSize, null, null, version, null));
} else if (msgType.equals("PREPARE")) {
String query = line.substring(8);
return new PrepareMessage(query, null);
} else if (msgType.equals("EXECUTE")) {
try {
byte[] preparedStatementId = Hex.hexToBytes(iter.next());
byte[] resultMetadataId = Hex.hexToBytes(iter.next());
List<ByteBuffer> values = new ArrayList<ByteBuffer>();
while (iter.hasNext()) {
String next = iter.next();
ByteBuffer bb;
try {
int v = Integer.parseInt(next);
bb = Int32Type.instance.decompose(v);
} catch (NumberFormatException e) {
bb = UTF8Type.instance.decompose(next);
}
values.add(bb);
}
return new ExecuteMessage(MD5Digest.wrap(preparedStatementId), MD5Digest.wrap(resultMetadataId), QueryOptions.forInternalCalls(ConsistencyLevel.ONE, values));
} catch (Exception e) {
return null;
}
} else if (msgType.equals("OPTIONS")) {
return new OptionsMessage();
} else if (msgType.equals("AUTHENTICATE")) {
Map<String, String> credentials = readCredentials(iter);
if (!credentials.containsKey(PasswordAuthenticator.USERNAME_KEY) || !credentials.containsKey(PasswordAuthenticator.PASSWORD_KEY)) {
System.err.println("[ERROR] Authentication requires both 'username' and 'password'");
return null;
}
return new AuthResponse(encodeCredentialsForSasl(credentials));
} else if (msgType.equals("REGISTER")) {
String type = line.substring(9).toUpperCase();
try {
return new RegisterMessage(Collections.singletonList(Enum.valueOf(Event.Type.class, type)));
} catch (IllegalArgumentException e) {
System.err.println("[ERROR] Unknown event type: " + type);
return null;
}
}
return null;
}
use of com.google.common.base.Splitter in project Plume by tdunning.
the class WordCountTest method countWords.
private <T extends CharSequence> void countWords(PCollection<T> lines) {
final Splitter onNonWordChar = Splitter.on(CharMatcher.BREAKING_WHITESPACE);
PCollection<String> words = lines.map(new DoFn<T, String>() {
@Override
public void process(T x, EmitFn<String> emitter) {
for (String word : onNonWordChar.split(x.toString())) {
emitter.emit(word);
}
}
}, collectionOf(strings()));
PTable<String, Integer> wc = words.map(new DoFn<String, Pair<String, Integer>>() {
@Override
public void process(String x, EmitFn<Pair<String, Integer>> emitter) {
emitter.emit(Pair.create(x, 1));
}
}, tableOf(strings(), integers())).groupByKey().combine(new CombinerFn<Integer>() {
@Override
public Integer combine(Iterable<Integer> counts) {
int sum = 0;
for (Integer k : counts) {
sum += k;
}
return sum;
}
});
Map<String, Integer> m = Maps.newHashMap();
for (Pair<String, Integer> pair : wc) {
m.put(pair.getKey(), pair.getValue());
}
assertEquals(3, m.get("is").intValue());
assertEquals(3, m.get("some").intValue());
assertEquals(3, m.get("simple").intValue());
assertEquals(1, m.get("examples").intValue());
assertEquals(2, m.get("text").intValue());
}
use of com.google.common.base.Splitter in project symja_android_library by axkr.
the class StringMapFunctions method countTokens.
default DoubleColumn countTokens(String separator) {
DoubleColumn newColumn = DoubleColumn.create(name() + "[token count]", this.size());
for (int r = 0; r < size(); r++) {
String value = getString(r);
Splitter splitter = Splitter.on(separator);
splitter = splitter.trimResults();
splitter = splitter.omitEmptyStrings();
List<String> tokens = new ArrayList<>(splitter.splitToList(value));
newColumn.set(r, tokens.size());
}
return newColumn;
}
Aggregations