Skip to content

Commit

Permalink
Added javadoc and test for Optionals utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
LesTR committed Feb 16, 2019
1 parent 3ab3c08 commit d39df73
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/main/java/cz/o2/proxima/util/Optionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@

import java.util.Optional;

/**
* Utility class for manipulation with {@link Optional}
*/
public class Optionals {

/**
* Get value from Optional or throw IllegalArgumentException
*
* @param optional Optional object
* @param <T> Generic type Optional
* @return T value from Optional
* @throws IllegalArgumentException in case of empty value
*/
public static <T> T get(Optional<T> optional) {
return optional.orElseThrow(() ->
new IllegalArgumentException("Provided optional is empty."));
Expand Down
39 changes: 39 additions & 0 deletions core/src/test/java/cz/o2/proxima/util/OptionalsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2017-2019 O2 Czech Republic, a.s.
*
* 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 cz.o2.proxima.util;

import org.junit.Test;

import java.util.Optional;

import static org.junit.Assert.assertEquals;

/**
* Tests for utility class {@link Optionals}
*/
public class OptionalsTest {

@Test
public void testGet() {
String foo = "bar";
assertEquals(foo, Optionals.get(Optional.of(foo)));
}

@Test(expected = IllegalArgumentException.class)
public void testGetFromEmpty() {
Optionals.get(Optional.empty());
}
}

0 comments on commit d39df73

Please sign in to comment.