public abstract class HeaderMenu {
private final PageFetcher fetcher;
private final User user;
public HeaderMenu(final PageFetcher fetcher, final User user) {
this.fetcher = fetcher;
this.user = user;
}
public final MenuItem build() {
Optional<Item> featured = getFeaturedItem(user);
List<Link> headers = getHeaders(user);
List<HeaderSubItem> columns =
header.stream()
.map(link -> new HeaderSubItem(link, fetcher.getPages(link.getUrl()))
.collect(Collectors.toList());
return new MenuItem(featured, columns);
}
protected abstract Optional<Item> getFeaturedItem(User user);
protected abstract List<Link> getHeaders(User user);
}
public final class StoreUserHeaderMenu extends HeaderMenu {
public StoreUserHeaderMenu(User user) {
super(PageFetcher.RELATED_PAGES_FETCHER, user);
}
@Override
proteced Optional<Item> getFeaturedItem(final User user) {
// get latest article
}
@Override
protected List<Link> getHeaders(final User user) {
// Get About Us, Community, Department,
// Store and Human Resources links
}
}
public class AdministrativeHeaderMenu extends HeaderMenu {
public CorporateHeaderMenu(User user) {
super(PageFetcher.RELATED_PAGES_FETCHER, user);
}
@Override
proteced Optional<Item> getFeaturedItem(final User user) {
// get upcoming staff meeting
}
@Override
protected List<Link> getHeaders(final User user) {
// Get About Us, Community, Department,
// Human Resources links -- No Store Header!
}
}
public final class CorporateHeaderMenu extends AdministrativeHeaderMenu {
public CorporateHeaderMenu(User user) {
super(user);
}
@Override
proteced Optional<Item> getFeaturedItem(final User user) {
// get upcoming shareholder meeting
}
}
public enum PageFetcher {
CHILD_FETCHER {
@Override
public List<Link> getPages(URI path) {
// pull direct pages under given path
}
},
RELATED_PAGES_FETCHER {
@Override
public List<Link> getPages(URI path) {
// pull pages that are related to page on given path
}
};
public abstract List<Link> getPages(final URI path);
}
public final class HeaderMenuFactory {
private HeaderMenuFactory() {
throw new AssertionError("This class shouldn't be initialized");
}
public static HeaderMenu newInstance(final User user) {
switch (user.getType()) {
case STORE:
return new StoreUserHeaderMenu(user);
case ADMINISTRATIVE:
return new AdministrativeHeaderMenu(user);
case CORPORATE:
return new CorporateHeaderMenu(user);
default:
throw new AssertionError();
}
}
}
import java.util.ArrayList;
public class Array
{
private ArrayList<Object> a = new ArrayList<Object>();
public void add(Object element)
{
a.add(element);
}
public void addAll(Object elements[])
{
for (int i = 0; i < elements.length; ++i)
a.add(elements[i]); // this line is going to be changed
}
}
public class ArrayCount extends Array
{
private int count = 0;
@Override
public void add(Object element)
{
super.add(element);
++count;
}
@Override
public void addAll(Object elements[])
{
super.addAll(elements);
count += elements.length;
}
}
public class Point {
private double x;
private double y;
// more code
public double getX() { return x; }
public double getY() { return y; }
}
public class Circle extends Point {
private double radius;
public Circle(public double radius, double x, double y) {
super(x, y);
this.radius = radius;
}
// no need to write getters for x and y
public double getRadius() {
return radius;
}
}
public class Circle {
private Point point;
private double radius;
public Circle(Point point, double radius) {
this.point = point;
this.radius = radius;
}
public double getX() {
return point.getX(); // re use
}
public double getY() {
return point.getX(); // re use
}
}
public class ForwardingSet<E> implements Set<E> {
private final Set<E> s;
public ForwardingSet(Set<E> s) { this.s = s; }
public void clear() { s.clear(); }
public boolean contains(Object o) { return s.contains(o); }
public boolean isEmpty() { return s.isEmpty(); }
public int size() { return s.size(); }
public Iterator<E> iterator() { return s.iterator(); }
public boolean add(E e) { return s.add(e); }
public boolean remove(Object o) { return s.remove(o); }
public boolean containsAll(Collection<?> c) { return s.containsAll(c); }
public boolean addAll(Collection<? extends E> c) { return s.addAll(c); }
public boolean removeAll(Collection<?> c) { return s.removeAll(c); }
public boolean retainAll(Collection<?> c) { return s.retainAll(c); }
public Object[] toArray() { return s.toArray(); }
public <T> T[] toArray(T[] a) { return s.toArray(a); }
@Override public boolean equals(Object o) { return s.equals(o); }
@Override public int hashCode() { return s.hashCode(); }
@Override public String toString() { return s.toString(); }
}
public class InstrumentedSet<E> extends ForwardingSet<E> {
private int addCount = 0;
public InstrumentedSet(Set<E> s) {
super(s);
}
@Override
public boolean add(E e) {
addCount++;
return super.add(e);
}
@Override
public boolean addAll(Collection<? extends E> c) {
addCount += c.size();
return super.addAll(c);
}
public int getAddCount() {
return addCount;
}
}
An API should be easy to use and hard to misuse. It should be easy to do simple things; possible to do complex things; and impossible, or at least difficult, to do wrong things.
-- Joshua Bloch
public int factorial(int n) {
return n * factorial(n - 1);
}
public int factorial(final int n) {
int f = 1;
while(n-- > 0) {
f *= n;
}
return f;
}
private int loop(final int n, final int accumulator) {
return n == 0 ? accumulator : loop(n - 1, accumulator * n);
}
public int factorial(final int n) {
return loop(n, 1);
}
--Edsger W. Dijkstra
-- Alan Kay
-- Bjarne Stroustrup