├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── NOTICE
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── metamx
│ ├── common
│ ├── ByteBufferUtils.java
│ ├── CompressionUtils.java
│ ├── FileUtils.java
│ ├── Granularity.java
│ ├── IAE.java
│ ├── ISE.java
│ ├── JodaUtils.java
│ ├── MapUtils.java
│ ├── MappedByteBufferHandler.java
│ ├── Pair.java
│ ├── Props.java
│ ├── RE.java
│ ├── RetryUtils.java
│ ├── StreamUtils.java
│ ├── StringUtils.java
│ ├── Timing.java
│ ├── UOE.java
│ ├── collect
│ │ ├── AggregatingMap.java
│ │ ├── CountingMap.java
│ │ ├── MoreIterators.java
│ │ └── Utils.java
│ ├── concurrent
│ │ ├── ExecutorServiceConfig.java
│ │ ├── ExecutorServices.java
│ │ ├── FunctionalThreadFactory.java
│ │ ├── ScheduledExecutorFactory.java
│ │ ├── ScheduledExecutors.java
│ │ └── SimpleExecutorConfig.java
│ ├── config
│ │ ├── Config.java
│ │ └── DurationCoercible.java
│ ├── guava
│ │ ├── Accumulator.java
│ │ ├── Accumulators.java
│ │ ├── BaseSequence.java
│ │ ├── CloseQuietly.java
│ │ ├── Comparators.java
│ │ ├── ConcatSequence.java
│ │ ├── DefaultingHashMap.java
│ │ ├── DelegatingYieldingAccumulator.java
│ │ ├── DroppingIterable.java
│ │ ├── DroppingIterator.java
│ │ ├── ExecuteWhenDoneYielder.java
│ │ ├── ExecutorExecutingSequence.java
│ │ ├── FilteredSequence.java
│ │ ├── FilteringAccumulator.java
│ │ ├── FilteringYieldingAccumulator.java
│ │ ├── Fns.java
│ │ ├── FunctionalIterable.java
│ │ ├── FunctionalIterator.java
│ │ ├── IteratorWithBaggage.java
│ │ ├── LazySequence.java
│ │ ├── LimitedSequence.java
│ │ ├── LimitedYieldingAccumulator.java
│ │ ├── MappedSequence.java
│ │ ├── MappingAccumulator.java
│ │ ├── MappingYieldingAccumulator.java
│ │ ├── MergeIterable.java
│ │ ├── MergeIterator.java
│ │ ├── MergeSequence.java
│ │ ├── ResourceClosingSequence.java
│ │ ├── ResourceClosingYielder.java
│ │ ├── Sequence.java
│ │ ├── Sequences.java
│ │ ├── SimpleSequence.java
│ │ ├── YieldSign.java
│ │ ├── Yielder.java
│ │ ├── Yielders.java
│ │ ├── YieldingAccumulator.java
│ │ ├── YieldingAccumulators.java
│ │ ├── YieldingSequenceBase.java
│ │ └── nary
│ │ │ ├── BinaryFn.java
│ │ │ ├── BinaryTransformIterable.java
│ │ │ ├── BinaryTransformIterator.java
│ │ │ ├── SortedMergeIterable.java
│ │ │ ├── SortedMergeIterator.java
│ │ │ ├── TrinaryFn.java
│ │ │ ├── TrinaryTransformIterable.java
│ │ │ └── TrinaryTransformIterator.java
│ ├── io
│ │ └── smoosh
│ │ │ ├── FileSmoosher.java
│ │ │ ├── Metadata.java
│ │ │ ├── Smoosh.java
│ │ │ ├── SmooshedFileMapper.java
│ │ │ └── SmooshedWriter.java
│ ├── lifecycle
│ │ ├── Lifecycle.java
│ │ ├── LifecycleStart.java
│ │ └── LifecycleStop.java
│ ├── logger
│ │ └── Logger.java
│ └── parsers
│ │ ├── CSVParser.java
│ │ ├── CloseableIterator.java
│ │ ├── DelimitedParser.java
│ │ ├── JSONParser.java
│ │ ├── JSONPathParser.java
│ │ ├── JSONToLowerParser.java
│ │ ├── JavaScriptParser.java
│ │ ├── ParseException.java
│ │ ├── Parser.java
│ │ ├── ParserUtils.java
│ │ ├── Parsers.java
│ │ ├── RegexParser.java
│ │ ├── TimestampParser.java
│ │ └── ToLowerCaseParser.java
│ ├── emitter
│ ├── EmittingLogger.java
│ ├── core
│ │ ├── BaseHttpEmittingConfig.java
│ │ ├── Batch.java
│ │ ├── BatchingStrategy.java
│ │ ├── ComposingEmitter.java
│ │ ├── ComposingEmitterConfig.java
│ │ ├── ConcurrentTimeCounter.java
│ │ ├── ContentEncoding.java
│ │ ├── EmittedBatchCounter.java
│ │ ├── Emitter.java
│ │ ├── Emitters.java
│ │ ├── Event.java
│ │ ├── FeedUriExtractor.java
│ │ ├── HttpEmitterConfig.java
│ │ ├── HttpPostEmitter.java
│ │ ├── JacksonUtil.java
│ │ ├── LoggingEmitter.java
│ │ ├── LoggingEmitterConfig.java
│ │ ├── NoopEmitter.java
│ │ ├── ParametrizedUriEmitter.java
│ │ ├── ParametrizedUriEmitterConfig.java
│ │ ├── ParametrizedUriExtractor.java
│ │ ├── UriExtractor.java
│ │ ├── ZeroCopyByteArrayOutputStream.java
│ │ ├── factory
│ │ │ ├── ComposingEmitterFactory.java
│ │ │ ├── EmitterFactory.java
│ │ │ ├── HttpEmitterFactory.java
│ │ │ ├── LoggingEmitterFactory.java
│ │ │ ├── NoopEmiterFactory.java
│ │ │ └── ParametrizedUriEmitterFactory.java
│ │ ├── filter
│ │ │ ├── BWListEventFilter.java
│ │ │ ├── BWListEventFilterConfig.java
│ │ │ ├── BWListEventFilterFactory.java
│ │ │ ├── EmptyBWListEventFilter.java
│ │ │ ├── FeedBWListEventFilter.java
│ │ │ └── MultiBWListEventFilter.java
│ │ └── statsd
│ │ │ ├── DimensionConverter.java
│ │ │ ├── StatsDEmitter.java
│ │ │ ├── StatsDEmitterConfig.java
│ │ │ ├── StatsDEmitterFactory.java
│ │ │ └── StatsDMetric.java
│ └── service
│ │ ├── AlertBuilder.java
│ │ ├── AlertEvent.java
│ │ ├── ServiceEmitter.java
│ │ ├── ServiceEvent.java
│ │ ├── ServiceEventBuilder.java
│ │ └── ServiceMetricEvent.java
│ ├── http
│ └── client
│ │ ├── AbstractHttpClient.java
│ │ ├── CredentialedHttpClient.java
│ │ ├── EnforceSslHttpClient.java
│ │ ├── HttpClient.java
│ │ ├── HttpClientConfig.java
│ │ ├── HttpClientInit.java
│ │ ├── NettyHttpClient.java
│ │ ├── Request.java
│ │ ├── auth
│ │ ├── BasicCredentials.java
│ │ └── Credentials.java
│ │ ├── io
│ │ └── AppendableByteArrayInputStream.java
│ │ ├── netty
│ │ └── HttpClientPipelineFactory.java
│ │ ├── pool
│ │ ├── ChannelResourceFactory.java
│ │ ├── ResourceContainer.java
│ │ ├── ResourceFactory.java
│ │ ├── ResourcePool.java
│ │ ├── ResourcePoolConfig.java
│ │ └── ResourceVerifier.java
│ │ └── response
│ │ ├── ClientResponse.java
│ │ ├── FullResponseHandler.java
│ │ ├── FullResponseHolder.java
│ │ ├── HttpResponseHandler.java
│ │ ├── InputStreamResponseHandler.java
│ │ ├── SequenceInputStreamResponseHandler.java
│ │ ├── StatusResponseHandler.java
│ │ ├── StatusResponseHolder.java
│ │ └── ToStringResponseHandler.java
│ └── metrics
│ ├── AbstractMonitor.java
│ ├── CgroupUtil.java
│ ├── CompoundMonitor.java
│ ├── CpuAcctDeltaMonitor.java
│ ├── FeedDefiningMonitor.java
│ ├── HttpPostEmitterMonitor.java
│ ├── JvmCpuMonitor.java
│ ├── JvmMonitor.java
│ ├── JvmPidDiscoverer.java
│ ├── JvmThreadsMonitor.java
│ ├── KeyedDiff.java
│ ├── Monitor.java
│ ├── MonitorOfTheMonitors.java
│ ├── MonitorScheduler.java
│ ├── MonitorSchedulerConfig.java
│ ├── MonitorUtils.java
│ ├── Monitors.java
│ ├── ParametrizedUriEmitterMonitor.java
│ ├── PidDiscoverer.java
│ ├── SigarPidDiscoverer.java
│ ├── SigarUtil.java
│ ├── SysMonitor.java
│ └── cgroups
│ ├── CgroupDiscoverer.java
│ ├── CpuAcct.java
│ ├── ProcCgroupDiscoverer.java
│ ├── ProcPidCgroupDiscoverer.java
│ └── ProcSelfCgroupDiscoverer.java
└── test
├── java
└── com
│ └── metamx
│ ├── common
│ ├── BufferUtils.java
│ ├── ByteBufferUtilsTest.java
│ ├── CompressionUtilsTest.java
│ ├── FileUtilsTest.java
│ ├── JodaUtilsTest.java
│ ├── RetryUtilsTest.java
│ ├── StreamUtilsTest.java
│ ├── StringUtilsTest.java
│ ├── TestGranularity.java
│ ├── guava
│ │ ├── BaseSequenceTest.java
│ │ ├── ComparatorsTest.java
│ │ ├── ConcatSequenceTest.java
│ │ ├── ExecutorExecutingSequenceTest.java
│ │ ├── FilteredSequenceTest.java
│ │ ├── FunctionalIterableTest.java
│ │ ├── FunctionalIteratorTest.java
│ │ ├── LimitedSequenceTest.java
│ │ ├── MappedSequenceTest.java
│ │ ├── MergeIteratorTest.java
│ │ ├── MergeSequenceTest.java
│ │ ├── ResourceClosingSequenceTest.java
│ │ ├── SequenceTestHelper.java
│ │ ├── TestSequence.java
│ │ ├── UnsupportedSequence.java
│ │ └── nary
│ │ │ └── SortedMergeIteratorTest.java
│ ├── io
│ │ └── smoosh
│ │ │ └── SmooshedFileMapperTest.java
│ ├── lifecycle
│ │ └── LifecycleTest.java
│ ├── logger
│ │ └── LoggerTest.java
│ └── parsers
│ │ ├── CSVParserTest.java
│ │ ├── DelimitedParserTest.java
│ │ ├── JSONParserTest.java
│ │ ├── JSONPathParserTest.java
│ │ ├── JavaScriptParserTest.java
│ │ ├── RegexParserTest.java
│ │ └── TimestampParserTest.java
│ ├── emitter
│ ├── core
│ │ ├── BWListEventFilterConfigTest.java
│ │ ├── ComposingEmitterTest.java
│ │ ├── ComposingEmitterWithParametrizedTest.java
│ │ ├── CustomEmitterFactoryTest.java
│ │ ├── EmitterTest.java
│ │ ├── EmittersTest.java
│ │ ├── GoHandler.java
│ │ ├── GoHandlers.java
│ │ ├── HttpEmitterConfigTest.java
│ │ ├── HttpEmitterTest.java
│ │ ├── HttpPostEmitterStressTest.java
│ │ ├── IntEvent.java
│ │ ├── LoggingEmitterConfigTest.java
│ │ ├── MapEvent.java
│ │ ├── MockHttpClient.java
│ │ ├── ParametrizedUriEmitterConfigTest.java
│ │ ├── ParametrizedUriEmitterTest.java
│ │ ├── filter
│ │ │ ├── EmptyBWListEventFilterTest.java
│ │ │ ├── FeedBWListEventFilterTest.java
│ │ │ └── MultiBWListEventFilterTest.java
│ │ └── statsd
│ │ │ ├── DimensionConverterTest.java
│ │ │ ├── StatsDEmitterConfigTest.java
│ │ │ └── StatsDEmitterTest.java
│ └── service
│ │ ├── AlertEventTest.java
│ │ ├── ServiceMetricEventTest.java
│ │ └── UnitEvent.java
│ ├── http
│ └── client
│ │ ├── AsyncHttpClientTest.java
│ │ ├── FriendlyServersTest.java
│ │ ├── JankyServersTest.java
│ │ ├── io
│ │ └── AppendableByteArrayInputStreamTest.java
│ │ ├── pool
│ │ └── ResourcePoolTest.java
│ │ └── response
│ │ └── SequenceInputStreamResponseHandlerTest.java
│ └── metrics
│ ├── CpuAcctDeltaMonitorTest.java
│ ├── GcNameTest.java
│ ├── JvmMonitorTest.java
│ ├── JvmPidDiscovererTest.java
│ ├── MonitorUtilsTest.java
│ ├── MonitorsTest.java
│ ├── SigarLoadTest.java
│ ├── SigarPidDiscovererTest.java
│ ├── StubServiceEmitter.java
│ └── cgroups
│ ├── CpuAcctTest.java
│ ├── ProcCgroupDiscovererTest.java
│ └── TestUtils.java
└── resources
├── cpuacct.usage_all
├── defaultMetricDimensions.json
├── keystore.jks
├── log4j.xml
├── loremipsum.txt
├── proc.cgroups
├── proc.mounts
└── proc.pid.cgroup
/.gitignore:
--------------------------------------------------------------------------------
1 | *.ipr
2 | *.iws
3 | *.iml
4 | target
5 | dist
6 | *.jar
7 | *.tar
8 | *.zip
9 | *.iml
10 | *.ipr
11 | .idea
12 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | dist: trusty
3 | sudo: false
4 |
5 | jdk:
6 | - oraclejdk8
7 |
8 | cache:
9 | directories:
10 | - $HOME/.m2
11 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | For code contributions, we do require that a contributor license agreement
2 | (CLA) is agreed to before we can accept the code. Our CLA is fashioned after
3 | the Apache CLA.
4 |
5 | ### Corporate Agreement
6 |
7 | For corporate contributions, please fill out and return the [Metamarkets Corporate
8 | CLA](https://docs.google.com/document/d/1fgAWpzJT5sVwXqSF9r00gMsoJlxZyw24Ifejb4oyyrc/edit?usp=sharing)
9 | to [druid-admin@metamarkets.com](mailto:druid-admin@metamarkets.com)
10 |
11 | ### Individual Agreement
12 |
13 | For individual contributions please fill out and return the [Metamarkets Individual
14 | CLA](https://docs.google.com/document/d/1tHE41iQuQmowLgkBeAe7YMt6EfVjo2hLncvV80bfONs/edit?usp=sharing)
15 | to [druid-admin@metamarkets.com](mailto:druid-admin@metamarkets.com)
16 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | java-util
2 | Copyright 2011-2017 Metamarkets Group Inc.
3 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/ByteBufferUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011-2016 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | import sun.misc.Cleaner;
20 | import sun.nio.ch.DirectBuffer;
21 |
22 | import java.nio.ByteBuffer;
23 | import java.nio.MappedByteBuffer;
24 |
25 | /**
26 | */
27 | public class ByteBufferUtils
28 | {
29 | /**
30 | * Releases memory held by the given direct ByteBuffer
31 | *
32 | * @param buffer buffer to free
33 | */
34 | public static void free(ByteBuffer buffer)
35 | {
36 | if (buffer.isDirect()) {
37 | clean((DirectBuffer) buffer);
38 | }
39 | }
40 |
41 |
42 | /**
43 | * Un-maps the given memory mapped file
44 | *
45 | * @param buffer buffer
46 | */
47 | public static void unmap(MappedByteBuffer buffer)
48 | {
49 | free(buffer);
50 | }
51 |
52 | private static void clean(DirectBuffer buffer)
53 | {
54 | final Cleaner cleaner = buffer.cleaner();
55 | if (cleaner != null) {
56 | cleaner.clean();
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/IAE.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | /**
20 | */
21 | public class IAE extends IllegalArgumentException
22 | {
23 | public IAE(String formatText, Object... arguments)
24 | {
25 | super(StringUtils.safeFormat(formatText, arguments));
26 | }
27 |
28 | public IAE(Throwable cause, String formatText, Object... arguments)
29 | {
30 | super(StringUtils.safeFormat(formatText, arguments), cause);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/ISE.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | /**
20 | */
21 | public class ISE extends IllegalStateException
22 | {
23 | public ISE(String formatText, Object... arguments)
24 | {
25 | super(StringUtils.safeFormat(formatText, arguments));
26 | }
27 |
28 | public ISE(Throwable cause, String formatText, Object... arguments)
29 | {
30 | super(StringUtils.safeFormat(formatText, arguments), cause);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/MappedByteBufferHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.metamx.common;
19 |
20 | import java.io.File;
21 | import java.nio.MappedByteBuffer;
22 |
23 | /**
24 | * Facilitates using try-with-resources with {@link MappedByteBuffer}s which don't implement {@link AutoCloseable}.
25 | *
26 | *
This interface is a specialization of {@code io.druid.collections.ResourceHandler}.
27 | * @see FileUtils#map(File)
28 | */
29 | public final class MappedByteBufferHandler implements AutoCloseable
30 | {
31 | private final MappedByteBuffer mappedByteBuffer;
32 |
33 | MappedByteBufferHandler(MappedByteBuffer mappedByteBuffer)
34 | {
35 | this.mappedByteBuffer = mappedByteBuffer;
36 | }
37 |
38 | /**
39 | * Returns the wrapped buffer.
40 | */
41 | public MappedByteBuffer get()
42 | {
43 | return mappedByteBuffer;
44 | }
45 |
46 | /**
47 | * Unmaps the wrapped buffer.
48 | */
49 | @Override
50 | public void close()
51 | {
52 | ByteBufferUtils.unmap(mappedByteBuffer);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/Props.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 - 2015 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | import java.io.FileInputStream;
20 | import java.io.IOException;
21 | import java.util.Map;
22 | import java.util.Properties;
23 |
24 | public class Props {
25 |
26 | public static Properties fromFilename(String filename) throws IOException {
27 | final Properties props = new Properties();
28 | props.load(new FileInputStream(filename));
29 | return props;
30 | }
31 |
32 | public static Properties fromEnvs(String prefix) {
33 | final Properties props = new Properties();
34 | for (Map.Entry env: System.getenv().entrySet()) {
35 | if (env.getKey().startsWith(prefix)) {
36 | props.put(env.getKey().replaceAll("_", "."), env.getValue());
37 | }
38 | }
39 | return props;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/RE.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | /**
20 | */
21 | public class RE extends RuntimeException
22 | {
23 | public RE(String formatText, Object... arguments)
24 | {
25 | super(StringUtils.safeFormat(formatText, arguments));
26 | }
27 |
28 | public RE(Throwable cause, String formatText, Object... arguments)
29 | {
30 | super(StringUtils.safeFormat(formatText, arguments), cause);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/Timing.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | import com.metamx.common.logger.Logger;
20 |
21 | import java.util.concurrent.Callable;
22 |
23 | /**
24 | */
25 | public class Timing {
26 | public static RetType timeBenchmarkWrapException(String prefix, Callable callable, final Logger log) {
27 | try {
28 | return timeBenchmark(prefix, callable, log);
29 | } catch (Exception e) {
30 | throw new RuntimeException(e);
31 | }
32 | }
33 |
34 |
35 | public static RetType timeBenchmark(String prefix, Callable callable, Logger log) throws Exception {
36 | RetType retVal;
37 |
38 | long startTime = System.currentTimeMillis();
39 | retVal = callable.call();
40 | long endTime = System.currentTimeMillis();
41 |
42 | log.info(String.format("%s completed %,d millis.", prefix, endTime - startTime));
43 |
44 | return retVal;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/UOE.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common;
18 |
19 | /**
20 | */
21 | public class UOE extends UnsupportedOperationException
22 | {
23 | public UOE(String formatText, Object... arguments)
24 | {
25 | super(StringUtils.safeFormat(formatText, arguments));
26 | }
27 |
28 | public UOE(Throwable cause, String formatText, Object... arguments)
29 | {
30 | super(StringUtils.safeFormat(formatText, arguments), cause);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/collect/AggregatingMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.collect;
18 |
19 | import java.util.HashMap;
20 |
21 | // Can't find a good way to abstract over which aggregator representation is used,
22 | // so I just pick Double/MutableDouble.
23 | public class AggregatingMap extends HashMap
24 | {
25 | public void add(K k, double n)
26 | {
27 | final Double value = get(k);
28 |
29 | if (value == null) {
30 | put(k, n);
31 | return;
32 | }
33 |
34 | put(k, value + n);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/collect/CountingMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.collect;
18 |
19 | import com.google.common.base.Function;
20 | import com.google.common.collect.Maps;
21 |
22 | import java.util.AbstractMap;
23 | import java.util.HashMap;
24 | import java.util.Set;
25 | import java.util.concurrent.atomic.AtomicLong;
26 |
27 | // Can't find a good way to abstract over which counter representation is used,
28 | // so I just pick Long/MutableLong.
29 | public class CountingMap extends AbstractMap
30 | {
31 | private final HashMap counts = new HashMap<>();
32 |
33 | public void add(K k, Long n)
34 | {
35 | if (!counts.containsKey(k)) {
36 | counts.put(k, new AtomicLong(0));
37 | }
38 | counts.get(k).addAndGet(n);
39 | }
40 |
41 | public Set> entrySet()
42 | {
43 | return Maps.transformValues(
44 | counts,
45 | new Function()
46 | {
47 | @Override
48 | public Long apply(AtomicLong n)
49 | {
50 | return n.get();
51 | }
52 | }
53 | ).entrySet();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/concurrent/ExecutorServiceConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.concurrent;
18 |
19 | import org.skife.config.Config;
20 | import org.skife.config.Default;
21 |
22 | /**
23 | */
24 | public abstract class ExecutorServiceConfig
25 | {
26 | @Config(value = "${base_path}.formatString")
27 | @Default("processing-%s")
28 | public abstract String getFormatString();
29 |
30 | @Config(value = "${base_path}.numThreads")
31 | public int getNumThreads()
32 | {
33 | return Runtime.getRuntime().availableProcessors() - 1;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/concurrent/ExecutorServices.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.concurrent;
18 |
19 | import com.google.common.base.Throwables;
20 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
21 | import com.metamx.common.lifecycle.Lifecycle;
22 |
23 | import java.util.concurrent.ExecutorService;
24 | import java.util.concurrent.Executors;
25 |
26 | public class ExecutorServices
27 | {
28 | public static ExecutorService create(Lifecycle lifecycle, ExecutorServiceConfig config)
29 | {
30 | return manageLifecycle(
31 | lifecycle,
32 | Executors.newFixedThreadPool(
33 | config.getNumThreads(),
34 | new ThreadFactoryBuilder().setDaemon(true).setNameFormat(config.getFormatString()).build()
35 | )
36 | );
37 | }
38 |
39 | public static T manageLifecycle(Lifecycle lifecycle, final T service)
40 | {
41 | try {
42 | lifecycle.addMaybeStartHandler(
43 | new Lifecycle.Handler()
44 | {
45 | @Override
46 | public void start() throws Exception
47 | {
48 | }
49 |
50 | @Override
51 | public void stop()
52 | {
53 | service.shutdownNow();
54 | }
55 | }
56 | );
57 | }
58 | catch (Exception e) {
59 | Throwables.propagate(e);
60 | }
61 | return service;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/concurrent/ScheduledExecutorFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.concurrent;
18 |
19 | import java.util.concurrent.ScheduledExecutorService;
20 |
21 | public interface ScheduledExecutorFactory
22 | {
23 | public ScheduledExecutorService create(int corePoolSize, String nameFormat);
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/concurrent/SimpleExecutorConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.concurrent;
18 |
19 | /**
20 | */
21 | public class SimpleExecutorConfig extends ExecutorServiceConfig
22 | {
23 | private final String formatString;
24 | private final int numThreads;
25 |
26 | public SimpleExecutorConfig(
27 | String formatString,
28 | int numThreads
29 | )
30 | {
31 | this.formatString = formatString;
32 | this.numThreads = numThreads;
33 | }
34 |
35 | @Override
36 | public String getFormatString()
37 | {
38 | return formatString;
39 | }
40 |
41 | @Override
42 | public int getNumThreads()
43 | {
44 | return numThreads;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/config/Config.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.config;
18 |
19 | import org.skife.config.ConfigurationObjectFactory;
20 |
21 | import java.util.Properties;
22 |
23 | /**
24 | */
25 | public class Config
26 | {
27 | public static ConfigurationObjectFactory createFactory(Properties props)
28 | {
29 | ConfigurationObjectFactory configFactory = new ConfigurationObjectFactory(props);
30 | configFactory.addCoercible(new DurationCoercible());
31 | return configFactory;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/config/DurationCoercible.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.config;
18 |
19 | import org.joda.time.Duration;
20 | import org.joda.time.Period;
21 | import org.skife.config.Coercer;
22 | import org.skife.config.Coercible;
23 |
24 | /**
25 | */
26 | public class DurationCoercible implements Coercible
27 | {
28 | @Override
29 | public Coercer accept(Class> clazz)
30 | {
31 | if (Duration.class != clazz) {
32 | return null;
33 | }
34 |
35 | return new Coercer()
36 | {
37 | @Override
38 | public Duration coerce(String value)
39 | {
40 | return new Period(value).toStandardDuration();
41 | }
42 | };
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/Accumulator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | /**
20 | */
21 | public interface Accumulator
22 | {
23 | public AccumulatedType accumulate(AccumulatedType accumulated, InType in);
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/Accumulators.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | */
23 | public class Accumulators
24 | {
25 | public static , T> Accumulator list()
26 | {
27 | return new Accumulator()
28 | {
29 | @Override
30 | public ListType accumulate(ListType accumulated, T in)
31 | {
32 | accumulated.add(in);
33 | return accumulated;
34 | }
35 | };
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/CloseQuietly.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import com.metamx.common.logger.Logger;
20 |
21 | import java.io.Closeable;
22 | import java.io.IOException;
23 |
24 | /**
25 | */
26 | public class CloseQuietly
27 | {
28 | private static final Logger log = new Logger(CloseQuietly.class);
29 |
30 | public static void close(Closeable closeable)
31 | {
32 | if (closeable == null) {
33 | return;
34 | }
35 | try {
36 | closeable.close();
37 | }
38 | catch (IOException e) {
39 | log.error(e, "IOException thrown while closing Closeable.");
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/DefaultingHashMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import com.google.common.base.Supplier;
20 |
21 | import java.util.HashMap;
22 |
23 | /**
24 | */
25 | public class DefaultingHashMap extends HashMap
26 | {
27 | private final Supplier supplier;
28 |
29 | public DefaultingHashMap(
30 | Supplier supplier
31 | )
32 | {
33 | this.supplier = supplier;
34 | }
35 |
36 | @Override
37 | public V get(Object o)
38 | {
39 | V retVal = super.get(o);
40 |
41 | if (retVal == null) {
42 | retVal = supplier.get();
43 | super.put((K) o, retVal);
44 | }
45 |
46 | return retVal;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/DelegatingYieldingAccumulator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | /**
20 | */
21 | public class DelegatingYieldingAccumulator extends YieldingAccumulator
22 | {
23 | private final YieldingAccumulator delegate;
24 |
25 | public DelegatingYieldingAccumulator(
26 | YieldingAccumulator delegate
27 | )
28 | {
29 | this.delegate = delegate;
30 | }
31 |
32 | @Override
33 | public void yield()
34 | {
35 | delegate.yield();
36 | }
37 |
38 | @Override
39 | public boolean yielded()
40 | {
41 | return delegate.yielded();
42 | }
43 |
44 | @Override
45 | public void reset()
46 | {
47 | delegate.reset();
48 | }
49 |
50 | @Override
51 | public OutType accumulate(OutType accumulated, T in)
52 | {
53 | return delegate.accumulate(accumulated, in);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/DroppingIterable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import java.util.Iterator;
20 |
21 | /**
22 | */
23 | public class DroppingIterable implements Iterable
24 | {
25 | private final Iterable delegate;
26 | private final int numToDrop;
27 |
28 | public DroppingIterable(
29 | Iterable delegate,
30 | int numToDrop
31 | )
32 | {
33 | this.delegate = delegate;
34 | this.numToDrop = numToDrop;
35 | }
36 |
37 | public Iterator iterator()
38 | {
39 | return new DroppingIterator<>(delegate.iterator(), numToDrop);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/DroppingIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import java.util.Iterator;
20 |
21 | /**
22 | */
23 | public class DroppingIterator implements Iterator
24 | {
25 | private final Iterator delegate;
26 | private final int numToDrop;
27 | private boolean dropped = false;
28 |
29 | public DroppingIterator(
30 | Iterator delegate,
31 | int numToDrop
32 | )
33 | {
34 | this.delegate = delegate;
35 | this.numToDrop = numToDrop;
36 | }
37 |
38 | public boolean hasNext()
39 | {
40 | if (! dropped) {
41 | for (int i = 0; i < numToDrop; ++i) {
42 | delegate.next();
43 | }
44 | dropped = true;
45 | }
46 |
47 | return delegate.hasNext();
48 | }
49 |
50 | public T next()
51 | {
52 | if (! dropped) {
53 | for (int i = 0; i < numToDrop; ++i) {
54 | delegate.next();
55 | }
56 | dropped = true;
57 | }
58 | return delegate.next();
59 | }
60 |
61 | public void remove()
62 | {
63 | throw new UnsupportedOperationException();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/ExecuteWhenDoneYielder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | package com.metamx.common.guava;
19 |
20 | import java.io.IOException;
21 | import java.util.concurrent.Executor;
22 |
23 | public class ExecuteWhenDoneYielder implements Yielder
24 | {
25 | private final Yielder baseYielder;
26 | private final Runnable runnable;
27 | private final Executor executor;
28 |
29 | public ExecuteWhenDoneYielder(Yielder baseYielder, Runnable runnable, Executor executor)
30 | {
31 | this.baseYielder = baseYielder;
32 | this.runnable = runnable;
33 | this.executor = executor;
34 | }
35 |
36 | @Override
37 | public T get()
38 | {
39 | return baseYielder.get();
40 | }
41 |
42 | @Override
43 | public Yielder next(T initValue)
44 | {
45 | return new ExecuteWhenDoneYielder<>(baseYielder.next(initValue), runnable, executor);
46 | }
47 |
48 | @Override
49 | public boolean isDone()
50 | {
51 | return baseYielder.isDone();
52 | }
53 |
54 | @Override
55 | public void close() throws IOException
56 | {
57 | if (isDone()) {
58 | executor.execute(runnable);
59 | }
60 | baseYielder.close();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/FilteringAccumulator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import com.google.common.base.Predicate;
20 |
21 | /**
22 | */
23 | public class FilteringAccumulator implements Accumulator
24 | {
25 | private final Predicate pred;
26 | private final Accumulator accumulator;
27 |
28 | public FilteringAccumulator(
29 | Predicate pred,
30 | Accumulator accumulator
31 | ) {
32 | this.pred = pred;
33 | this.accumulator = accumulator;
34 | }
35 |
36 | @Override
37 | public OutType accumulate(OutType accumulated, T in)
38 | {
39 | if (pred.apply(in)) {
40 | return accumulator.accumulate(accumulated, in);
41 | }
42 | return accumulated;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/FilteringYieldingAccumulator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import com.google.common.base.Predicate;
20 |
21 | /**
22 | */
23 | public class FilteringYieldingAccumulator extends YieldingAccumulator
24 | {
25 | private final Predicate pred;
26 | private final YieldingAccumulator accumulator;
27 |
28 | private volatile boolean didSomething = false;
29 |
30 | public FilteringYieldingAccumulator(
31 | Predicate pred,
32 | YieldingAccumulator accumulator
33 | ) {
34 | this.pred = pred;
35 | this.accumulator = accumulator;
36 | }
37 |
38 | @Override
39 | public void yield()
40 | {
41 | accumulator.yield();
42 | }
43 |
44 | @Override
45 | public boolean yielded()
46 | {
47 | return accumulator.yielded();
48 | }
49 |
50 | @Override
51 | public void reset()
52 | {
53 | didSomething = false;
54 | accumulator.reset();
55 | }
56 |
57 | public boolean didSomething()
58 | {
59 | return didSomething;
60 | }
61 |
62 | @Override
63 | public OutType accumulate(OutType accumulated, T in)
64 | {
65 | if (pred.apply(in)) {
66 | if (!didSomething) {
67 | didSomething = true;
68 | }
69 | return accumulator.accumulate(accumulated, in);
70 | }
71 | return accumulated;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/com/metamx/common/guava/Fns.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011,2012 Metamarkets Group Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.metamx.common.guava;
18 |
19 | import com.google.common.base.Function;
20 |
21 | import java.util.Map;
22 |
23 | /**
24 | */
25 | public class Fns
26 | {
27 | public static Function splitFn(final String splitChar, final int numCols)
28 | {
29 | return new Function()
30 | {
31 | public String[] apply(String input)
32 | {
33 | return input.split(splitChar, numCols);
34 | }
35 | };
36 | }
37 |
38 | public static Function