what's the difference between resource.adaptTo(ValueMap.class) and resource.getValueMap()? | Community
Skip to main content
Keerthi0555
Level 5
March 31, 2023
Solved

what's the difference between resource.adaptTo(ValueMap.class) and resource.getValueMap()?

  • March 31, 2023
  • 1 reply
  • 1041 views

what's the difference between resource.adaptTo(ValueMap.class) and resource.getValueMap()?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @keerthi0555,

The main difference is that adaptTo can return null, so you will have to make sure to implement some null check while using it, to avoid NPE.

getValueMap() method is null safe and will return empty ValueMap in worst case.

In terms of getting ValueMap, you can also check ResourceUtil.getValueMap(resource) that will return empty ValueMap even in the scenario when give resource is null.

Some links for reference:

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
April 2, 2023

Hi @keerthi0555,

The main difference is that adaptTo can return null, so you will have to make sure to implement some null check while using it, to avoid NPE.

getValueMap() method is null safe and will return empty ValueMap in worst case.

In terms of getting ValueMap, you can also check ResourceUtil.getValueMap(resource) that will return empty ValueMap even in the scenario when give resource is null.

Some links for reference:

Keerthi0555
Level 5
April 3, 2023

Thank you .