@Override public byte[] apply(T t) { return SerDeUtils.toBytes(t); } }
/** * Deserializes the given bytes. * * @param bytes the function argument * @return the function result */ @Override public T apply(byte[] bytes) { return fromBytes(bytes, clazz); } }
/** * Returns the default value of the configuration, cast to the expected type. * * @param clazz The class of the expected type of the configuration value. * @param <T> The expected type of the configuration value. */ public <T> T getDefault(Class<T> clazz) { return defaultValue == null ? null: ConversionUtils.convert(defaultValue, clazz); }
@Override public Object apply(List<Object> args) { int expectedInsertions = 100000; float falsePositiveRate = 0.01f; if (args.size() > 1) { expectedInsertions = ConversionUtils.convert(args.get(0), Integer.class); } if (args.size() > 2) { falsePositiveRate = ConversionUtils.convert(args.get(1), Float.class); } return new BloomFilter<>(SerDeUtils.SERIALIZER, expectedInsertions, falsePositiveRate); } }
@Test @SuppressWarnings("unchecked") public void testBloomFilter() { final BloomFilter<Object> expected = new BloomFilter<>(new BloomFilter.DefaultSerializer<>(), 10000, 0.01); expected.add("foo"); expected.add("bar"); byte[] raw = SerDeUtils.toBytes(expected); BloomFilter<Object> actual = (BloomFilter) SerDeUtils.fromBytes(raw, Object.class); Assert.assertTrue(actual.mightContain("foo")); Assert.assertFalse(actual.mightContain("timothy")); assertEquals(expected, actual); }
@Override public void logAccess(LookupKey key) { numInsertions++; filter.add(key); }
@Test public void testArbitraryPojo() { final ArbitraryPojo expected = new ArbitraryPojo(); byte[] raw = SerDeUtils.toBytes(expected); Object actual = SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); } }
@Override public boolean hasSeen(LookupKey key) { return filter.mightContain(key); }
public BloomAccessTracker(String name, int expectedInsertions, double falsePositiveRate) { this.name = name; this.expectedInsertions = expectedInsertions; this.falsePositiveRate = falsePositiveRate; filter = new BloomFilter<LookupKey>(new LookupKeySerializer(), expectedInsertions, falsePositiveRate); } public BloomAccessTracker() {}
@Test public void urlToPortTest() { runWithArguments("URL_TO_PORT", "http://www.google.com/foo/bar", 80); runWithArguments("URL_TO_PORT", "https://www.google.com/foo/bar", 443); runWithArguments("URL_TO_PORT", "http://www.google.com:7979/foo/bar", 7979); }
/** * Create directory that is automatically cleaned up after the * JVM shuts down through use of a Runtime shutdown hook. * * @param dir Directory to create, including missing parent directories * @return File handle to the created temp directory */ public static File createTempDir(File dir) throws IOException { return createTempDir(dir, true); }
public static void verboseLogging() { verboseLogging("%d [%p|%c|%C{1}] %m%n", Level.ALL); }
/** * @return The default value of the property. */ public <T> T getDefault(Class<T> clazz) { return ConversionUtils.convert(defaultValue, clazz); }
/** * Serializes the given Object into bytes. * */ @Override public byte[] apply(Object o) { return toBytes(o); } }
@Test public void urlToProtocolTest() { runWithArguments("URL_TO_PROTOCOL", "http://www.google.com/foo/bar", "http"); runWithArguments("URL_TO_PROTOCOL", "https://www.google.com/foo/bar", "https"); }
/** * Create directory that is automatically cleaned up after the * JVM shuts down through use of a Runtime shutdown hook. * * @param prefix Prefix to apply to temp directory name * @return File handle to the created temp directory * @throws IOException Unable to create temp directory */ public static File createTempDir(String prefix) throws IOException { return createTempDir(prefix, true); }
public <T> T getDefault(Class<T> clazz) { return defaultValue == null?null:ConversionUtils.convert(defaultValue, clazz); }
/** * Retrieves the property value from a map of properties. * @param properties A map of properties. * @return The value of the property within the map. */ public <T> T get(Map<Object, Object> properties, Class<T> clazz) { Object o = properties.getOrDefault(key, defaultValue); return o == null ? null : ConversionUtils.convert(o, clazz); }
@Override public Object apply(List<Object> strings ) { return strings.get(0) == null?null: ConversionUtils.convert(strings.get(0), clazz); } }
public <T> T getAndConvert(Optional<String> configPrefix, Map<String, Object> config, Class<T> clazz) { Object o = get(configPrefix, config); if(o != null) { return ConversionUtils.convert(o, clazz); } return null; } }