class ChordSelector implements IChordSelector {

    @Override
    public HiveChord selectFourthChord(HarmonicLoop selectedLoop, HiveChord thirdChord, HiveChord nextChord, List<HiveChord> hiveChordInDatabase) {

        List<HiveChord> candidates = getChordCandidatesBetweenChords(thirdChord, nextChord, hiveChordInDatabase);

        if (candidates == null || candidates.size() == 0) {
            return null;
        }

        List<HiveChord> compatibleChords = selectedLoop.getValidChords();

        List<HiveChord> perfectMatches = new ArrayList<>();

        for (HiveChord chord : compatibleChords) {
            for (HiveChord dbChord : candidates) {
                if (chord.isEqual(dbChord)) {
                    perfectMatches.add(dbChord);
                }
            }
        }

        if (perfectMatches.size() > 0) {
            int index = new Random().nextInt(perfectMatches.size());
            return perfectMatches.get(index);
        } else {
            int index = new Random().nextInt(compatibleChords.size());
            return compatibleChords.get(index);
        }
    }

}