ResolveState (@NotNull ResolveState state) { if (state == ourInitialState) { return this; } else if (state instanceof OneElementResolveState) { OneElementResolveState oneState = (OneElementResolveState)state; return put(oneState.myKey, oneState.myValue); } boolean has1 = state.get(myKey1) != null; boolean has2 = state.get(myKey2) != null; if (has1 && has2) { return state; } else if (has1) { return state.put(myKey2, myValue2); } else if (has2) { return state.put(myKey1, myValue1); } // at this point our keys are not in other and other has at least 2 keys return new ManyElementResolveState(state, this); }