Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:34:45+00:00 2026-05-27T00:34:45+00:00

I have a point object: class Point { final int x,y; … } Because

  • 0

I have a point object:

  class Point {
    final int x,y;
    ...
  }

Because these points will be used/created all over the place in my code, I want to start using guavas cache. Unfortuinally the CacheLoader only accept one parameter.
Another question here on stackoverflow use a pair object for a similar problem. But I don’t like the idea to create a dummy object for every cache request.
So I come up with my own workaround:

Because the object is specified by the x and y, I think I can merge(shift) both values into a long, which will be my key.

void test(int x, int y) {
    Long key = (long) ((long) (x) << Integer.SIZE | y);
    Point point = cache.get(key);
}

CacheLoader<Long, Point> loader = new CacheLoader<Long, Point>() {
    public Point load(Long key) throws Exception {
    final int x,y;
        // shift magic
        x = (int) (key >> Integer.SIZE);
        y = key.intValue();
        return new Point(x, y);
    }
};

I’m actually a shift noob. Will this work? Did I miss something? Is this “faster” than the pair class? That’s my question!

Yes, I test the code and it works so far I can tell.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:34:46+00:00Added an answer on May 27, 2026 at 12:34 am

    How about this? Your Point class must correctly implement equals() and hashcode().

    static class Points {
      static final Interner<Point> INTERNER = Interners.newStrongInterner();
    
      public static Point cached(final int x, final int y) {
        return INTERNER.intern(new Point(x, y));
      }
    }
    

    Your actual purpose was to cache equal objects, right? Than this will suffice your needs.
    Usage:

    Point center = Points.cached(0, 0);
    

    Or an adjusted version of your cache example:

    CacheLoader<Point, Point> loader = new CacheLoader<Point, Point>() {
      @Override
      public Point load(final Point point) {
        return point;
      }
    }
    ...
    Point center = cache.get(new Point(0, 0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, class MyClass { private int val; public static final MyClass
I have GoogleMaps-object. Also I have SouthWest and NorthEast point in GLatLng-format. Also I
In C++ I have a reference to an object that wants to point back
I have a very simple array (please focus on the object with "points.bean.pointsBase" as
Let's say I have 3 point clouds: first that has 3 points {x1,y1,z1}, {x2,y2,z2},
It is said that when we have a class Point and knows how to
I have a simple custom Point class as follows and I would like to
I have a class that is created from an enum that can be tested
I have a list of Point objects, (each one with x,y properties) and would
I have a DateTime object that I need to print in a custom gridlike

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.