Ways to encapsulate the selection of a Java primitive; Avoiding "magic" primitives

I am writing a program that creates a large number of large arrays for storing data. All this data needs to be stored in RAM, so I avoid objects and currently use shorts to save space. These shorts serve as identification numbers that can be placed in the search class to get the corresponding object on demand. I recently asked a question about whether I need only 2 bytes of short code, and so now I wonder if one way or another, to determine the type of data stored in one place in my code so that I can easily change it, track down each type, return type, etc. that is currently set to short.

If I were ready to use objects, I could just simply do

class MySmallNumber extends Short{} 

and change the parent class if necessary.

If it is C / C ++, I could use

 #define small short 

for the effect I'm looking for.

I am looking for a way to do something like this in java, which does not require storing 64-bit object references in my arrays. Any help is appreciated. Now I am looking at a really dirty IDE to do this.

+4
source share
3 answers

I would suggest decomposing all the code, which depends on the type of your identification values, into a separate class. Let this class handle all operations (including search), which depend on whether the ID values ​​are short , byte or something else. You can pass individual values ​​in short or even int values, even if they are internally converted to byte . (This is, for example, how java.io.DataOutputStream.writeByte(int) was written - it takes an int argument and treats it as a byte value.)

+1
source

You can encapsulate an array in some custom class. This should not increase overhead because you are working with large arrays.

In all other places in your code, you can use long. When you pass these long strings to your own array, you can convert it to the one you use inside it.

Finally, you should only make changes to this class.

+2
source

not quite sure that you are here, but this may be of interest:

 import java.util.Arrays; interface Index { short getIndex(int i); void setIndex(int i, short value); int size(); } class ShortIndexImpl implements Index { ShortIndexImpl(int n) { indices = new short[n]; } @Override public short getIndex(int i) { return indices[i]; } @Override public void setIndex(int i, short value) { indices[i] = value; } @Override public int size() { return indices.length; } final short[] indices; } class TenBitIndexImpl implements Index { TenBitIndexImpl(int n) { indices = new int[(n + 2) / 3]; } @Override public short getIndex(int i) { int index = i / 3; int remainder = i % 3; int word = indices[index]; return (short) (0x3ff & (word >> shifts[remainder])); } @Override public void setIndex(int i, short value) { int index = i / 3; int remainder = i % 3; int word = indices[index] & ~masks[remainder]; int shiftedValue = ((int) value) << shifts[remainder]; word |= shiftedValue; indices[index] = word; } @Override public int size() { return indices.length; } final int masks[] = new int[] { 0x3ff00000, 0xffc00, 0x3ff }; final int shifts[] = new int[] { 20, 10, 0 }; final int[] indices; } public class Main { static void test(Index index) { for (int i = 0; i < values.length; i++) index.setIndex(i, values[i]); for (int i = 0; i < values.length; i++) { System.out.println(values[i] + " " + index.getIndex(i)); if (index.getIndex(i) != values[i]) System.out.println("expected " + values[i] + " but got " + index.getIndex(i)); } } public static void main(String[] args) { Index index = new ShortIndexImpl(values.length); test(index); index = new TenBitIndexImpl(values.length); test(index); System.out.println("indices"); for (int i = 0; i < ((TenBitIndexImpl) index).indices.length; i++) System.out.println(((TenBitIndexImpl) index).indices[i]); } static short[] values = new short[] { 1, 2, 3, 4, 5, 6 }; } 
0
source

Source: https://habr.com/ru/post/1391911/


All Articles