Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add toDate toHashCode function #885

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: java
dist: trusty
jdk:
- oraclejdk8
2 changes: 2 additions & 0 deletions jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public abstract class Modifier implements SpecDriven, ContextualTransform {

STOCK_FUNCTIONS.put( "toInteger", new Objects.toInteger() );
STOCK_FUNCTIONS.put( "toDouble", new Objects.toDouble() );
STOCK_FUNCTIONS.put( "toDate", new Objects.toDate() );
STOCK_FUNCTIONS.put( "toHashCode", new Objects.toHashCode() );
STOCK_FUNCTIONS.put( "toLong", new Objects.toLong() );
STOCK_FUNCTIONS.put( "toBoolean", new Objects.toBoolean() );
STOCK_FUNCTIONS.put( "toString", new Objects.toString() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import com.bazaarvoice.jolt.common.Optional;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -39,6 +43,7 @@ public class Objects {
* also, see: MathTest#testNitPicks
*
*/

public static Optional<? extends Number> toNumber(Object arg) {
if ( arg instanceof Number ) {
return Optional.of( ( (Number) arg ));
Expand Down Expand Up @@ -84,6 +89,53 @@ else if(arg instanceof String) {
return Optional.empty();
}
}

//return date
public static Optional<String> toDate(final List args) {

if ( args == null || args.size() < 2 || args.size() > 3 ) {
return Optional.empty();
}

if ( ! (( args.get(0) instanceof Long || args.get(0) instanceof Integer ) && args.get(1) instanceof String ) ) {
return Optional.empty();
}

if( args.size() == 3 && ! (args.get(2) instanceof String ) ) {
return Optional.empty();
}

DateTimeFormatter formatter = null;
Instant instant = null;
try {
instant = Instant.ofEpochMilli( Long.valueOf( String.valueOf( args.get(0) ) ) );
formatter = DateTimeFormatter.ofPattern((String) args.get(1));
} catch(Exception e) {
return Optional.empty();
}

ZoneId zone = null;
try {
zone = ZoneId.of((String) args.get(2));
} catch(Exception e) {
if(args.size() > 2) {
return Optional.empty();
} else {
zone = ZoneOffset.UTC;
}
}
return Optional.of(formatter.withZone(zone).format(instant));
}


public static Optional<Integer> toHashCode(Object arg) {
if ( arg instanceof String ) {
return Optional.of(arg.hashCode());
}
else {
return Optional.empty();
}
}

/**
* Returns long value of argument, if possible, wrapped in Optional
Expand Down Expand Up @@ -231,13 +283,28 @@ protected Optional<Long> applySingle( final Object arg ) {
}
}

public static final class toDate extends Function.ListFunction {
@Override
protected Optional<Object> applyList( final List<Object> argIntList ) {
return (Optional) toDate(argIntList);
}
}

public static final class toDouble extends Function.SingleFunction<Double> {
@Override
protected Optional<Double> applySingle( final Object arg ) {
return toDouble( arg );
}
}

public static final class toHashCode extends Function.SingleFunction<Integer> {
@Override
protected Optional<Integer> applySingle( final Object arg ) {
return toHashCode( arg );
}
}


public static final class toBoolean extends Function.SingleFunction<Boolean> {
@Override
protected Optional<Boolean> applySingle( final Object arg ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2013 Bazaarvoice, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bazaarvoice.jolt.modifier.function;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.testng.annotations.DataProvider;

import com.bazaarvoice.jolt.common.Optional;

@SuppressWarnings("deprecation")
public class JavaObjectTest extends AbstractTester {

@DataProvider(parallel = true)
public Iterator<Object[]> getTestCases() {
List<Object[]> testCases = new LinkedList<>( );

Function TO_DATE = new Objects.toDate();
Function TO_HASH_CODE = new Objects.toHashCode();

testCases.add( new Object[] {"null-inputs", TO_DATE, null, Optional.empty() } );
testCases.add( new Object[] {"empty-inputs", TO_DATE, Collections.emptyList(), Optional.empty() } );
testCases.add( new Object[] {"missing-format-input", TO_DATE, Arrays.asList(1572353915824L), Optional.empty() } );
testCases.add( new Object[] {"extra-inputs", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "UTC", 1000), Optional.empty() } );
testCases.add( new Object[] {"invalid-first-type", TO_DATE, Arrays.asList("yyyy-MM-dd HH:mm:ss.SSS","yyyy-MM-dd HH:mm:ss.SSS"), Optional.empty() } );
testCases.add( new Object[] {"invalid-second-type", TO_DATE, Arrays.asList(1572353915824L,1572353915824L), Optional.empty() } );
testCases.add( new Object[] {"invalid-second-format", TO_DATE, Arrays.asList(1572353915824L,"yyyy-yyyy-aa bb-cc-dd"), Optional.empty() } );
testCases.add( new Object[] {"invalid-third-format", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", 100), Optional.empty() } );
testCases.add( new Object[] {"invalid-third-format", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "U T C"), Optional.empty() } );
testCases.add( new Object[] {"invalid-first-int", TO_DATE, Arrays.asList(157235,"yyyy-MM-dd HH:mm:ss.SSS"), Optional.of("1970-01-01 00:02:37.235") } );
testCases.add( new Object[] {"valid-without-timezone", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS"), Optional.of("2019-10-29 12:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-Z", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "Z"), Optional.of("2019-10-29 12:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-UTC", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "UTC"), Optional.of("2019-10-29 12:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-GMT", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "GMT"), Optional.of("2019-10-29 12:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-GMT+1", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "GMT+1"), Optional.of("2019-10-29 13:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-GMT-10", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "GMT-10"), Optional.of("2019-10-29 02:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-GMT+03:30", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "GMT+03:30"), Optional.of("2019-10-29 16:28:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-UTC+02:00", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "UTC+02:00"), Optional.of("2019-10-29 14:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-UTC+02:00", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "UTC-02:00"), Optional.of("2019-10-29 10:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-+04", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "+04"), Optional.of("2019-10-29 16:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone-+05:00", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "+05:00"), Optional.of("2019-10-29 17:58:35.824") } );
testCases.add( new Object[] {"valid-with-timezone--05:00", TO_DATE, Arrays.asList(1572353915824L,"yyyy-MM-dd HH:mm:ss.SSS", "-05:00"), Optional.of("2019-10-29 07:58:35.824") } );

testCases.add( new Object[] { "hash-null", TO_HASH_CODE, null, Optional.empty() } );
testCases.add( new Object[] { "hash-invalid", TO_HASH_CODE, new Object(), Optional.empty() } );
testCases.add( new Object[] { "hash-empty-list", TO_HASH_CODE, new Object[] {}, Optional.empty() } );
testCases.add( new Object[] { "hash-empty-array", TO_HASH_CODE, Arrays.asList( ), Optional.empty() } );
testCases.add( new Object[] { "hash-empty-string", TO_HASH_CODE, "", Optional.of( "".hashCode() ) } );
testCases.add( new Object[] { "hash-empty-string", TO_HASH_CODE, "hashcode", Optional.of( "hashcode".hashCode() ) } );

return testCases.iterator();
}

}