Skip to content

Commit

Permalink
Shorter TreeStatePair methods by using Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Dec 11, 2016
1 parent c312645 commit 120b304
Showing 1 changed file with 10 additions and 19 deletions.
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;


import java.io.IOException; import java.io.IOException;
import java.util.Optional;


import org.neo4j.io.pagecache.PageCursor; import org.neo4j.io.pagecache.PageCursor;


Expand All @@ -44,53 +45,43 @@ static Pair<TreeState,TreeState> readStatePages( PageCursor cursor, long pageIdA


static TreeState selectNewestValidState( Pair<TreeState,TreeState> states ) static TreeState selectNewestValidState( Pair<TreeState,TreeState> states )
{ {
TreeState selected = selectNewestValidStateOrNull( states ); return selectNewestValidStateOptionally( states ).orElseThrow( () ->
if ( selected != null ) new TreeInconsistencyException( "Unexpected combination of state.%n STATE_A[%s]%n STATE_B[%s]",
{ states.getLeft(), states.getRight() ) );
return selected;
}

// Fail
throw new TreeInconsistencyException( "Unexpected combination of state.%n STATE_A[%s]%n STATE_B[%s]",
states.getLeft(), states.getRight() );
} }


static TreeState selectOldestOrInvalid( Pair<TreeState,TreeState> states ) static TreeState selectOldestOrInvalid( Pair<TreeState,TreeState> states )
{ {
TreeState newestValidState = selectNewestValidStateOrNull( states ); TreeState newestValidState = selectNewestValidStateOptionally( states ).orElse( states.getRight() );
if ( newestValidState == null )
{
return states.getLeft();
}
return newestValidState == states.getLeft() ? states.getRight() : states.getLeft(); return newestValidState == states.getLeft() ? states.getRight() : states.getLeft();
} }


private static TreeState selectNewestValidStateOrNull( Pair<TreeState,TreeState> states ) private static Optional<TreeState> selectNewestValidStateOptionally( Pair<TreeState,TreeState> states )
{ {
TreeState stateA = states.getLeft(); TreeState stateA = states.getLeft();
TreeState stateB = states.getRight(); TreeState stateB = states.getRight();


if ( stateA.isValid() != stateB.isValid() ) if ( stateA.isValid() != stateB.isValid() )
{ {
// return only valid // return only valid
return stateA.isValid() ? stateA : stateB; return stateA.isValid() ? Optional.of( stateA ) : Optional.of( stateB );
} }
else if ( stateA.isValid() && stateB.isValid() ) else if ( stateA.isValid() && stateB.isValid() )
{ {
// return newest // return newest
if ( stateA.stableGeneration() > stateB.stableGeneration() && if ( stateA.stableGeneration() > stateB.stableGeneration() &&
stateA.unstableGeneration() > stateB.unstableGeneration() ) stateA.unstableGeneration() > stateB.unstableGeneration() )
{ {
return stateA; return Optional.of( stateA );
} }
else if ( stateA.stableGeneration() < stateB.stableGeneration() && else if ( stateA.stableGeneration() < stateB.stableGeneration() &&
stateA.unstableGeneration() < stateB.unstableGeneration() ) stateA.unstableGeneration() < stateB.unstableGeneration() )
{ {
return stateB; return Optional.of( stateB );
} }
} }


// return null communicating that this combination didn't result in any valid "newest" state // return null communicating that this combination didn't result in any valid "newest" state
return null; return Optional.empty();
} }
} }

0 comments on commit 120b304

Please sign in to comment.