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 8474773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:42:51+00:00 2026-06-10T17:42:51+00:00

I am trying to design a generic Key class in java to represent primary

  • 0

I am trying to design a generic Key class in java to represent primary pojo keys.

The values can be of various types i.e. BigInteger, String, Uuid’s . I am looking for best option to implement such a class. My current implementation looks something like this.

Can anyone help me with a more accurate implementation or identify issues with the current implementation? I am yet to implement a equals method. Any pointers are welcome.

This class needs to be GWT compatible.

@SuppressWarnings("serial")
@GwtCompatible
public class Key<T extends Serializable> implements Serializable {

public enum KeyType {
    BigInt, Uuid, String ;

}

private final T keyValue  ;
private final KeyType keyType  ;

Key() {
    keyValue = null;
    keyType = null;
}

public Key(T value){
    this.keyValue =  value ;
    this.keyType = determineKeyType(value);
}

/**
 * @return
 */
private KeyType determineKeyType(Object value) {

    if ( isValueUuid(value))
        return KeyType.Uuid ;

    else if (value instanceof BigInteger)
        return KeyType.BigInt ;

    else 
        return KeyType.String; 
}

/**
 * @param value
 * @return
 */
private boolean isValueUuid(Object value) {
    // TODO Auto-generated method stub
    return false;
}

public Key(T val, KeyType tp){
    this.keyValue = val ;
    this.keyType = tp;
}

public KeyType getKeyType(){
    return keyType ;
}


@Override
public boolean equals(Object obj) {
    return super.equals(obj);
}

public T getKeyValue(){
    return this.keyValue ;
}
}
  • 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-06-10T17:42:53+00:00Added an answer on June 10, 2026 at 5:42 pm

    It looks to me me that you would benefit from a Factory pattern here.

    You would have an interface Key, an AbstractKey, and as many implementations of this AbstractKey as you want (in your example case, it should be 3). The KeyFactory would be in charge of creating the key.

    Practically, it would give:

    public interface Key<T> extends Serializable {
        T getKeyValue();
    }
    
    public abstract class AbstractKey<T> implements Key<T> {
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (obj instanceof AbstractKey) {
                return getKeyValue().equals(((AbstractKey) obj).getKeyValue());
            }
            return false;
        }
    }
    
    public class StringKey extends AbstractKey<String> {
    
        private String keyValue;
    
        StringKey() {
            super();
        }
    
        protected StringKey(String val) {
            super(val);
            this.keyValue = val;
        }
    
        @Override
        public String getKeyValue() {
            return keyValue;
        }
    }
    
    public class BigIntKey extends AbstractKey<BigInteger> {
    
        private BigInteger keyValue;
    
        BigIntKey() {
            super();
        }
    
        protected BigIntKey(BigInteger val) {
            super(val);
            this.keyValue = val;
        }
    
        @Override
        public BigInteger getKeyValue() {
            return keyValue;
        }
    }
    
    ...
    
    public class KeyFactory {
        public static Key<String> getKey(String obj) {
            return new StringKey(obj);
        }
    
        public static Key<BigInteger> getKey(BigInteger obj) {
            return new BigIntKey(obj);
        }
    }
    

    One advantage of this solution, even if it’s more verbose than the one you already have, is that you can restrict the types of your Keys to what you actually need. In addition, with constructors that have limited visibility (just enough to make GWT compile properly), you can enforce the code to use the KeyFactory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem trying to design some generic storage.. Basically I have the
I am trying to design my Class Diagram. But I get stuck on this
I am fairly new to various design structures and OOP. what i am trying
I am trying to design a class that will be flexible enough to chart
I'm trying to create a UserControl that inherits from a generic class. It does
I'm trying to design a Form class in PHP. So far, I have the
I am trying to design a location lookup in which the user can specify
I'm trying to design the architecture for a new LOB MVVM project utilising Caliburn
I am trying to design a listview with 3 textViews within it. 1 textview
I'm trying to design a theme on WordPress (version 3.3.2) but I am having

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.