class State { private String stateAbbreviation; private boolean expandedMedicade; public State(String stateAbbreviation, boolean expandedMedicade) { super(); this.stateAbbreviation = stateAbbreviation; this.expandedMedicade = expandedMedicade; } public String getStateAbbreviation() { return stateAbbreviation; } public boolean isExpandedMedicade() { return expandedMedicade; } @Override public String toString() { return Objects.toStringHelper(State.class) .add("stateAbbreviation", stateAbbreviation) .add("expandedMedicade", expandedMedicade) .toString(); } } class Message { private String stateAbbreviation; private String messageToResidence; public String getMessageToResidence() { return messageToResidence; } public void setMessageToResidence(String messageToResidence) { this.messageToResidence = messageToResidence; } public String getStateAbbreviation() { return stateAbbreviation; } public void setStateAbbreviation(String stateAbbreviation) { this.stateAbbreviation = stateAbbreviation; } @Override public String toString() { return Objects.toStringHelper(State.class) .add("stateAbbreviation", stateAbbreviation) .add("messageToResidence", messageToResidence) .toString(); } } // seed data List<State> states = Lists.newArrayList( new State("MT", false), new State("ID", false), new State("WY", false), new State("SD", false), new State("NE", false), new State("WI", false), new State("IN", false), new State("TX", false), new State("CA", true), new State("AZ", true), new State("NM", true), new State("AR", true), new State("IL", true), new State("IA", true)); Predicate<State> byExpandedMedicade = new Predicate<State>() { public boolean apply(State input) { return input.isExpandedMedicade(); } }; Function<State, Message> messages = new Function<State, Message>() { public Message apply(State state) { String stateCode = state.getStateAbbreviation(); Message message = new Message(); message.setStateAbbreviation(stateCode); message.setMessageToResidence("<a href='" + stateCode + "'>" + stateCode + "</a>"); return message; } };
/** * This test will filter a list of states and then transform * elements into messages * */ @Test public void filter_transform_list () { List<Message> stateMessages = FluentIterable.from(states) .filter(byExpandedMedicade) .transform(messages) .toList(); logger.info(stateMessages); assertEquals(6, stateMessages.size()); }
Output
[ State{stateAbbreviation=CA, messageToResidence=<a href='CA'>CA</a>}, State{stateAbbreviation=AZ, messageToResidence=<a href='AZ'>AZ</a>}, State{stateAbbreviation=NM, messageToResidence=<a href='NM'>NM</a>}, State{stateAbbreviation=AR, messageToResidence=<a href='AR'>AR</a>}, State{stateAbbreviation=IL, messageToResidence=<a href='IL'>IL</a>}, State{stateAbbreviation=IA, messageToResidence=<a href='IA'>IA</a>} ]