/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter9;
import java.util.function.Supplier;
/**
* Demonstrates how to use the Supplier interface to obtain
* card in the context of a card source that can supply an infinity
* of card.
*
* See Section 9.4
*/
public class InfiniteCardSource implements CardSource {
/**
* Code that illustrates how to use this class
*/
@SuppressWarnings("unused")
public static void main(String[] args) {
InfiniteCardSource randomCards = new InfiniteCardSource();
InfiniteCardSource acesOfHeart = new InfiniteCardSource();
}
private final Supplier<Card> aCardSupplier;
public InfiniteCardSource(Supplier<Card> pCardSupplier) {
aCardSupplier = pCardSupplier;
}
@Override
public Card draw() {
return aCardSupplier.get();
}
@Override
public boolean isEmpty() {
return false;
}
}
With this method reference, the InfiniteCardSource
will call Card.random()
whenever
a Card
is needed, thus generating an infinite sequence of random cards.
With this method reference, the InfiniteCardSource
will call Card.random()
whenever
a Card
is needed, thus generating an infinite sequence of random cards.
With this lambda expression, the InfiniteCardSource
will execute the right side of the expression
whenever a Card
is needed, thus always returning an ace of hearts.
With this lambda expression, the InfiniteCardSource
will execute the right side of the expression
whenever a Card
is needed, thus always returning an ace of hearts.
The SuppressWarnings
annotation interface is applicable in all declaration contexts, so an @SuppressWarnings
annotation can be used on any element. As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. For example, if you want to suppress a warning in a particular method, you should annotate that method rather than its class.
The set of warnings suppressed in a given element is a union of the warnings suppressed in all containing elements. For example, if you annotate a class to suppress one warning and annotate a method in the class to suppress another, both warnings will be suppressed in the method. However, note that if a warning is suppressed in a module-info
file, the suppression applies to elements within the file and not to types contained within the module. Likewise, if a warning is suppressed in a package-info
file, the suppression applies to elements within the file and not to types contained within the package.
Java compilers must recognize all the kinds of warnings defined in the Java Language Specification (JLS section 9.6.4.5) which include:
"unchecked"
. "deprecation"
. "removal"
. "preview"
. javac
reference implementation recognizes compilation-related warning names documented in its --help-lint
output.
There is no requirement that a new or distinct result be returned each time the supplier is invoked.
This is a functional interface
whose functional method is get()
.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
A String
represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
.
The String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator
class provides methods for finer-grain, locale-sensitive String comparison.
javac
compiler may implement the operator with StringBuffer
, StringBuilder
, or java.lang.invoke.StringConcatFactory
depending on the JDK version. The implementation of string conversion is typically through the method toString
, defined by Object
and inherited by all classes in Java.