Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
remove some indent
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
        private final List<E> peekedElements = new ArrayList<>();

        @Override
        public boolean hasNext() {
            Optional<E> peekElement = peek();
            return peekElement.isPresent();
        }

        @Override
        public E next() {
            if (!peekedElements.isEmpty()) {
                return peekedElements.remove(0);
            }
            return iterator.next();
        }

        @Override
        public boolean nextMatches(final Predicate<? super E> condition) {
            Objects.requireNonNull(condition, "condition");
            Optional<E> peekElement = peek();
            return (peekElement.isPresent() && condition.test(peekElement.get()));
        }

        /**
         * Returns an optional containing the next element, or an empty optional if there is none.
         *
         * @return  The optional containing the next element, or the empty optional if there is none.
         */
        private Optional<E> peek() {
            if (!peekedElements.isEmpty()) {
                return Optional.ofNullable(peekedElements.get(0));
            }
            if (!iterator.hasNext()) {
                return Optional.empty();
            }
            E element = iterator.next();
            peekedElements.add(element);
            return Optional.ofNullable(element);
        }
        private final List<E> peekedElements = new ArrayList<>();

        @Override
        public boolean hasNext() {
            Optional<E> peekElement = peek();
            return peekElement.isPresent();
        }

        @Override
        public E next() {
            if (!peekedElements.isEmpty()) {
                return peekedElements.remove(0);
            }
            return iterator.next();
        }

        @Override
        public boolean nextMatches(final Predicate<? super E> condition) {
            Objects.requireNonNull(condition, "condition");
            Optional<E> peekElement = peek();
            return (peekElement.isPresent() && condition.test(peekElement.get()));
        }

        /**
         * Returns an optional containing the next element, or an empty optional if there is none.
         *
         * @return  The optional containing the next element, or the empty optional if there is none.
         */
        private Optional<E> peek() {
            if (!peekedElements.isEmpty()) {
                return Optional.ofNullable(peekedElements.get(0));
            }
            if (!iterator.hasNext()) {
                return Optional.empty();
            }
            E element = iterator.next();
            peekedElements.add(element);
            return Optional.ofNullable(element);
        }
private final List<E> peekedElements = new ArrayList<>();

@Override
public boolean hasNext() {
    Optional<E> peekElement = peek();
    return peekElement.isPresent();
}

@Override
public E next() {
    if (!peekedElements.isEmpty()) {
        return peekedElements.remove(0);
    }
    return iterator.next();
}

@Override
public boolean nextMatches(final Predicate<? super E> condition) {
    Objects.requireNonNull(condition, "condition");
    Optional<E> peekElement = peek();
    return (peekElement.isPresent() && condition.test(peekElement.get()));
}

/**
 * Returns an optional containing the next element, or an empty optional if there is none.
 *
 * @return  The optional containing the next element, or the empty optional if there is none.
 */
private Optional<E> peek() {
    if (!peekedElements.isEmpty()) {
        return Optional.ofNullable(peekedElements.get(0));
    }
    if (!iterator.hasNext()) {
        return Optional.empty();
    }
    E element = iterator.next();
    peekedElements.add(element);
    return Optional.ofNullable(element);
}
Bounty Awarded with 500 reputation awarded by janos
design
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144

The Design

A LogReader reads a log, filters the unwanted stuff and converts it into LogEntries. According to your design.

That's a bit much. Split it up.

Make one LogReader that just reads the file and presents you with endless strings. Oh wait, that's a BufferedReader! I'd see the LogReader basically as an iterator wrapper for BufferedReader.

Make a LogParser that takes a Iterable<String> and parses them into LogEntries. Then make a LogFilter that takes a bunch of LogEntries and returns only the ones you want.

Because the input and the output of the LogFilter are the same type, it's now optional for a caller. This simplifies your API when it comes to simple tasks.


The Design

A LogReader reads a log, filters the unwanted stuff and converts it into LogEntries. According to your design.

That's a bit much. Split it up.

Make one LogReader that just reads the file and presents you with endless strings. Oh wait, that's a BufferedReader! I'd see the LogReader basically as an iterator wrapper for BufferedReader.

Make a LogParser that takes a Iterable<String> and parses them into LogEntries. Then make a LogFilter that takes a bunch of LogEntries and returns only the ones you want.

Because the input and the output of the LogFilter are the same type, it's now optional for a caller. This simplifies your API when it comes to simple tasks.


unneeded variable, class naming comes with holes
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
unneeded variable
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
objects requirenotnull
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 43 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
code remarks
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
code remarks
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
code remarks
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 172 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 182 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 111 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 111 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading