001/*
002 * Copyright 2013 Alex Kasko (alexkasko.com)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.alexkasko.unsafe.offheapstruct;
018
019import com.alexkasko.unsafe.offheap.OffHeapDisposableIterable;
020
021/**
022 * Base interface for off-heap struct (memory areas of equal sizes) collection.
023 *
024 * @author alexkasko
025 * Date: 7/3/13
026 */
027public interface OffHeapStructCollection extends OffHeapDisposableIterable<byte[]> {
028
029    /**
030     * Returns length of the single struct in bytes
031     *
032     * @return length of the single struct in bytes
033     */
034    int structLength();
035
036    /**
037     * Returns number of elements in this array
038     *
039     * @return number of elements in this array
040     */
041    long size();
042
043    /**
044     * Copies struct on specified index into specified buffer
045     *
046     * @param index array index
047     * @param buffer buffer to copy struct into
048     */
049    void get(long index, byte[] buffer);
050
051    /**
052     * Copies struct part on specified index into specified buffer
053     *
054     * @param index array index
055     * @param srcPos struct position in collection
056     * @param dest buffer to copy struct into
057     * @param destPos position in buffer
058     * @param length number of bytes to copy
059     */
060    void get(long index, int srcPos, byte[] dest, int destPos, int length);
061
062    /**
063     * Copies specified struct contents onto specified index
064     *
065     * @param index array index
066     * @param struct struct to copy into array
067     */
068    void set(long index, byte[] struct);
069
070    /**
071     * Gets byte from struct on specified index with specified offset
072     *
073     * @param index array index
074     * @param offset byte array index
075     * @return byte
076     */
077    byte getByte(long index, int offset);
078
079    /**
080     * Puts byte into struct onto specified index with specified offset
081     *
082     * @param index array index
083     * @param offset byte array index
084     * @param value value
085     */
086    void putByte(long index, int offset, byte value);
087
088    /**
089     * Gets one byte (stored as one signed byte) from struct on specified index
090     * with specified offset, converts it to unsigned and returns it as short
091     *
092     * @param index array index
093     * @param offset byte array index
094     * @return unsigned byte as short
095     */
096    short getUnsignedByte(long index, int offset);
097
098    /**
099     * Puts short with value from 0 to 255 inclusive into struct onto specified
100     * index with specified offset as one signed byte
101     *
102     * @param index array index
103     * @param offset byte array index
104     * @param value unsigned byte
105     */
106    void putUnsignedByte(long index, int offset, short value);
107
108    /**
109     * Gets two bytes as short from struct on specified index with specified offset
110     *
111     * @param index array index
112     * @param offset byte array offset
113     * @return short value
114     */
115    short getShort(long index, int offset);
116
117    /**
118     * Puts short into struct onto specified index with specified offset as two bytes
119     *
120     * @param index array index
121     * @param offset byte array offset
122     * @param value short value
123     */
124    void putShort(long index, int offset, short value);
125
126    /**
127     * Gets unsigned short (stored as two bytes) from struct on specified index
128     * with specified offset and returns it as int
129     *
130     * @param index array index
131     * @param offset byte array offset
132     * @return unsigned short as int
133     */
134    int getUnsignedShort(long index, int offset);
135
136    /**
137     * Puts int with value from 0 to 65535 inclusive into struct onto specified
138     * index with specified offset as two bytes
139     *
140     * @param index array index
141     * @param offset byte array offset
142     * @param value unsigned short as int
143     */
144    void putUnsignedShort(long index, int offset, int value);
145
146    /**
147     * Gets four bytes as int from struct on specified index with specified offset
148     *
149     * @param index array index
150     * @param offset byte array offset
151     * @return int value
152     */
153    int getInt(long index, int offset);
154
155    /**
156     * Puts int into struct onto specified index with specified offset as four bytes
157     *
158     * @param index array index
159     * @param offset byte array offset
160     * @param value int value
161     */
162    void putInt(long index, int offset, int value);
163
164    /**
165     * Gets unsigned int (stored as 4 bytes) and returns it as long
166     * from struct on specified index with specified offset
167     *
168     * @param index array index
169     * @param offset byte array offset
170     * @return unsigned int as long
171     */
172    long getUnsignedInt(long index, int offset);
173
174    /**
175     * Puts long value from 0 to 4294967295 inclusive into struct onto specified index
176     * with specified offset as four bytes
177     *
178     * @param index array index
179     * @param offset byte array offset
180     * @param value unsigned int as long
181     */
182    void putUnsignedInt(long index, int offset, long value);
183
184    /**
185     * Gets long from struct on specified index with specified offset
186     *
187     * @param index array index
188     * @param offset byte array offset
189     * @return long value
190     */
191    long getLong(long index, int offset);
192
193    /**
194     * Puts long into struct onto specified index with specified offset as eight bytes
195     *
196     * @param index array index
197     * @param offset byte array offset
198     * @param value long value
199     */
200    void putLong(long index, int offset, long value);
201}