8184693: add Pattern.isEmpty

Added method Pattern.isEmpty

Reviewed-by: forax, chegar, smarks, psandoz, rriggs
This commit is contained in:
Vivek Theeyarath 2018-04-18 10:30:57 -07:00
parent 03a10ec7b2
commit 702ac597ad
8 changed files with 56 additions and 4 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -159,6 +159,17 @@ public final class Optional<T> {
return value != null; return value != null;
} }
/**
* If a value is not present, returns {@code true}, otherwise
* {@code false}.
*
* @return {@code true} if a value is not present, otherwise {@code false}
* @since 11
*/
public boolean isEmpty() {
return value == null;
}
/** /**
* If a value is present, performs the given action with the value, * If a value is present, performs the given action with the value,
* otherwise does nothing. * otherwise does nothing.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -139,6 +139,17 @@ public final class OptionalDouble {
return isPresent; return isPresent;
} }
/**
* If a value is not present, returns {@code true}, otherwise
* {@code false}.
*
* @return {@code true} if a value is not present, otherwise {@code false}
* @since 11
*/
public boolean isEmpty() {
return !isPresent;
}
/** /**
* If a value is present, performs the given action with the value, * If a value is present, performs the given action with the value,
* otherwise does nothing. * otherwise does nothing.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -139,6 +139,17 @@ public final class OptionalInt {
return isPresent; return isPresent;
} }
/**
* If a value is not present, returns {@code true}, otherwise
* {@code false}.
*
* @return {@code true} if a value is not present, otherwise {@code false}
* @since 11
*/
public boolean isEmpty() {
return !isPresent;
}
/** /**
* If a value is present, performs the given action with the value, * If a value is present, performs the given action with the value,
* otherwise does nothing. * otherwise does nothing.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -139,6 +139,17 @@ public final class OptionalLong {
return isPresent; return isPresent;
} }
/**
* If a value is not present, returns {@code true}, otherwise
* {@code false}.
*
* @return {@code true} if a value is not present, otherwise {@code false}
* @since 11
*/
public boolean isEmpty() {
return !isPresent;
}
/** /**
* If a value is present, performs the given action with the value, * If a value is present, performs the given action with the value,
* otherwise does nothing. * otherwise does nothing.

View file

@ -52,6 +52,7 @@ public class Basic {
assertFalse(empty.equals("unexpected")); assertFalse(empty.equals("unexpected"));
assertFalse(empty.isPresent()); assertFalse(empty.isPresent());
assertTrue(empty.isEmpty());
assertEquals(empty.hashCode(), 0); assertEquals(empty.hashCode(), 0);
assertEquals(empty.orElse("x"), "x"); assertEquals(empty.orElse("x"), "x");
assertEquals(empty.orElseGet(() -> "y"), "y"); assertEquals(empty.orElseGet(() -> "y"), "y");
@ -87,6 +88,7 @@ public class Basic {
assertFalse(opt.equals("unexpected")); assertFalse(opt.equals("unexpected"));
assertTrue(opt.isPresent()); assertTrue(opt.isPresent());
assertFalse(opt.isEmpty());
assertEquals(opt.hashCode(), expected.hashCode()); assertEquals(opt.hashCode(), expected.hashCode());
assertEquals(opt.orElse("unexpected"), expected); assertEquals(opt.orElse("unexpected"), expected);
assertEquals(opt.orElseGet(() -> "unexpected"), expected); assertEquals(opt.orElseGet(() -> "unexpected"), expected);

View file

@ -51,6 +51,7 @@ public class BasicDouble {
assertFalse(empty.equals("unexpected")); assertFalse(empty.equals("unexpected"));
assertFalse(empty.isPresent()); assertFalse(empty.isPresent());
assertTrue(empty.isEmpty());
assertEquals(empty.hashCode(), 0); assertEquals(empty.hashCode(), 0);
assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
@ -86,6 +87,7 @@ public class BasicDouble {
assertFalse(opt.equals("unexpected")); assertFalse(opt.equals("unexpected"));
assertTrue(opt.isPresent()); assertTrue(opt.isPresent());
assertFalse(opt.isEmpty());
assertEquals(opt.hashCode(), Double.hashCode(expected)); assertEquals(opt.hashCode(), Double.hashCode(expected));
assertEquals(opt.orElse(UNEXPECTED), expected); assertEquals(opt.orElse(UNEXPECTED), expected);
assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);

View file

@ -52,6 +52,7 @@ public class BasicInt {
assertFalse(empty.equals("unexpected")); assertFalse(empty.equals("unexpected"));
assertFalse(empty.isPresent()); assertFalse(empty.isPresent());
assertTrue(empty.isEmpty());
assertEquals(empty.hashCode(), 0); assertEquals(empty.hashCode(), 0);
assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
@ -87,6 +88,7 @@ public class BasicInt {
assertFalse(opt.equals("unexpected")); assertFalse(opt.equals("unexpected"));
assertTrue(opt.isPresent()); assertTrue(opt.isPresent());
assertFalse(opt.isEmpty());
assertEquals(opt.hashCode(), Integer.hashCode(expected)); assertEquals(opt.hashCode(), Integer.hashCode(expected));
assertEquals(opt.orElse(UNEXPECTED), expected); assertEquals(opt.orElse(UNEXPECTED), expected);
assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);

View file

@ -51,6 +51,7 @@ public class BasicLong {
assertFalse(empty.equals("unexpected")); assertFalse(empty.equals("unexpected"));
assertFalse(empty.isPresent()); assertFalse(empty.isPresent());
assertTrue(empty.isEmpty());
assertEquals(empty.hashCode(), 0); assertEquals(empty.hashCode(), 0);
assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
@ -86,6 +87,7 @@ public class BasicLong {
assertFalse(opt.equals("unexpected")); assertFalse(opt.equals("unexpected"));
assertTrue(opt.isPresent()); assertTrue(opt.isPresent());
assertFalse(opt.isEmpty());
assertEquals(opt.hashCode(), Long.hashCode(expected)); assertEquals(opt.hashCode(), Long.hashCode(expected));
assertEquals(opt.orElse(UNEXPECTED), expected); assertEquals(opt.orElse(UNEXPECTED), expected);
assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);