@Test public void test_getAttribute_otherClass() { CEKRequestMessage.Session session = new CEKRequestMessage.Session(); String sessionName = RandomStringUtils.randomAlphabetic(10); AImpl sessionValue = new AImpl(RandomStringUtils.randomAlphabetic(10)); Map<String, Object> sessionAttributes = new HashMap<>(); sessionAttributes.put(sessionName, sessionValue); session.setSessionAttributes(sessionAttributes); SessionHolder sessionHolder = new SessionHolder(objectMapper, session); assertThat(sessionHolder.getAttribute(sessionName, AImpl.class)).isEqualTo(sessionValue); assertThat(sessionHolder.getAttribute(sessionName, AImpl.class)).isEqualTo(sessionValue); assertThat(sessionHolder.getAttribute(sessionName, ABImpl.class)).satisfies(ab -> { assertThat(ab.getAValue()).isEqualTo(sessionValue.getAValue()); assertThat(ab.getBValue()).isNull(); }); }
@SuppressWarnings("unchecked") public <T> T getAttribute(@NonNull String name, @NonNull Class<T> type) { return (T) sessionCache.compute(name, (key, cachedVal) -> { if (cachedVal != null && type.isAssignableFrom(cachedVal.getClass())) { return cachedVal; } Object val = sessionAttributes.get(key); if (val == null) { return null; } return objectMapper.convertValue(val, type); }); }
SessionHolder { @SuppressWarnings("unchecked") public <T> T getAttribute(@NonNull String name, @NonNull Class<T> type) { return (T) sessionCache.compute(name, (key, cachedVal) -> { if (cachedVal != null && type.isAssignableFrom(cachedVal.getClass())) { return cachedVal; } Object val = sessionAttributes.get(key); if (val == null) { return null; } return objectMapper.convertValue(val, type); }); } }
SessionHolder { @SuppressWarnings("unchecked") public <T> T getAttribute(@NonNull String name, @NonNull Class<T> type) { return (T) sessionCache.compute(name, (key, cachedVal) -> { if (cachedVal != null && type.isAssignableFrom(cachedVal.getClass())) { return cachedVal; } Object val = sessionAttributes.get(key); if (val == null) { return null; } return objectMapper.convertValue(val, type); }); } SessionHolder(@NonNull ObjectMapper objectMapper, @NonNull Session session); }
SessionHolder { @SuppressWarnings("unchecked") public <T> T getAttribute(@NonNull String name, @NonNull Class<T> type) { return (T) sessionCache.compute(name, (key, cachedVal) -> { if (cachedVal != null && type.isAssignableFrom(cachedVal.getClass())) { return cachedVal; } Object val = sessionAttributes.get(key); if (val == null) { return null; } return objectMapper.convertValue(val, type); }); } SessionHolder(@NonNull ObjectMapper objectMapper, @NonNull Session session); @SuppressWarnings("unchecked") T getAttribute(@NonNull String name, @NonNull Class<T> type); T getAttribute(@NonNull String name, @NonNull TypeReference<T> typeReference); void setAttribute(@NonNull String name, Object value); void removeAttribute(@NonNull String name); }
SessionHolder { @SuppressWarnings("unchecked") public <T> T getAttribute(@NonNull String name, @NonNull Class<T> type) { return (T) sessionCache.compute(name, (key, cachedVal) -> { if (cachedVal != null && type.isAssignableFrom(cachedVal.getClass())) { return cachedVal; } Object val = sessionAttributes.get(key); if (val == null) { return null; } return objectMapper.convertValue(val, type); }); } SessionHolder(@NonNull ObjectMapper objectMapper, @NonNull Session session); @SuppressWarnings("unchecked") T getAttribute(@NonNull String name, @NonNull Class<T> type); T getAttribute(@NonNull String name, @NonNull TypeReference<T> typeReference); void setAttribute(@NonNull String name, Object value); void removeAttribute(@NonNull String name);  }