/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter9;
import java.util.List;
import java.util.function.Function;
/**
* Demonstration code for the Strategy pattern applied
* using functional-style design. See Section 9.5.
*/
public class AutoPlayer
{
private Function<List<Card>, Card> ;
public AutoPlayer(Function<List<Card>, Card> pSelectionStrategy) {
aSelectionStrategy = pSelectionStrategy;
}
@SuppressWarnings("unused")
public void play() {
Card selected = aSelectionStrategy.apply(getCards());
/* ... */
}
// Gets the cards to supply to the strategy
private List<Card> getCards() {
return null; // this is a stub
}
}
The Function
functional interface serves as an , here.
The Function
functional interface serves as an , here.
The interface that specifies the behavior of a strategy in the Strategy design pattern.
The interface that specifies the behavior of a strategy in the Strategy design pattern.
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1
and e2
such that e1.equals(e2)
, and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
The List
interface places additional stipulations, beyond those specified in the Collection
interface, on the contracts of the iterator
, add
, remove
, equals
, and hashCode
methods. Declarations for other inherited methods are also included here for convenience.
The List
interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList
class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List
interface provides a special iterator, called a ListIterator
, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator
interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
The List
interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.
The List
interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.
Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals
and hashCode
methods are no longer well defined on such a list.
Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException
or ClassCastException
. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.
The List.of
and List.copyOf
static factory methods provide a convenient way to create unmodifiable lists. The List
instances created by these methods have the following characteristics:
UnsupportedOperationException
to be thrown. However, if the contained elements are themselves mutable, this may cause the List's contents to appear to change. null
elements. Attempts to create them with null
elements result in NullPointerException
. subList
views implement the RandomAccess
interface. This interface is a member of the Java Collections Framework.
This is a functional interface
whose functional method is apply(Object)
.
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.