Skip to content

Commit

Permalink
Added methods to parse to Optional using exceptions. Upgraded to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
robtimus committed Jul 14, 2019
1 parent cb400e9 commit 4265eb6
Show file tree
Hide file tree
Showing 13 changed files with 893 additions and 234 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Currently there are five sets of methods:
* `TryParse.tryParseLong`: a copy of `Long.parseLong` to parse to [OptionalLong](https://docs.oracle.com/javase/8/docs/api/java/util/OptionalLong.html) instead of throwing exceptions.
* `TryParse.tryParseUnsignedLong`: a copy of `Long.parseUnsignedLong` to parse to [OptionalLong](https://docs.oracle.com/javase/8/docs/api/java/util/OptionalLong.html) instead of throwing exceptions.
* `TryParse.tryParseBoolean`: a copy of `Boolean.parseBoolean` to parse to [Optional](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html) instead of returning `false`.

In addition, there are several methods that delegate to an existing method, catching expected exceptions. These are not as fast as the above five sets, but can still be useful.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<version.java>1.8</version.java>

<version.hamcrest>2.1</version.hamcrest>
<version.junit>4.12</version.junit>
<version.junit>5.4.2</version.junit>
<version.mockito>2.28.2</version.mockito>

<version.plugin.antrun>1.8</version.plugin.antrun>
Expand All @@ -96,8 +96,8 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* ExceptionBasedParsing.java
* Copyright 2019 Rob Spoor
*
* 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.github.robtimus.tryparse;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that a method delegates to an existing method and catches its exceptions.
*
* @author Rob Spoor
* @since 1.1
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface ExceptionBasedParsing {
// marker annotation
}
37 changes: 37 additions & 0 deletions src/main/java/com/github/robtimus/tryparse/NativeParsing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* NativeParsing.java
* Copyright 2019 Rob Spoor
*
* 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.github.robtimus.tryparse;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that a method uses native parsing instead of delegating to an existing method and catching its exceptions.
*
* @author Rob Spoor
* @since 1.1
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface NativeParsing {
// marker annotation
}
39 changes: 39 additions & 0 deletions src/main/java/com/github/robtimus/tryparse/ParseFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* ParseFunction.java
* Copyright 2019 Rob Spoor
*
* 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.github.robtimus.tryparse;

/**
* A function that parses one object into another.
*
* @author Rob Spoor
* @param <T> The type of object to parse.
* @param <R> The type of the parse result.
* @param <X> The type of exception that occurs if parsing fails.
* @since 1.1
*/
public interface ParseFunction<T, R, X extends Exception> {

/**
* Parses an object.
*
* @param input The object to parse.
* @return The parsed object.
* @throws X If parsing fails.
*/
R parse(T input) throws X;
}
Loading

0 comments on commit 4265eb6

Please sign in to comment.