项目作者: Pyknic

项目描述 :
Read-only primitive Java arrays backed by Direct Buffers and indexed using 64-bit indexes
高级语言: Java
项目地址: git://github.com/Pyknic/immutable-array.git
创建时间: 2016-11-16T23:20:53Z
项目社区:https://github.com/Pyknic/immutable-array

开源协议:Other

下载


Immutable Array

Javadocs

Read-only primitive Java arrays backed by Direct Buffers and indexed using 64-bit indexes

The library uses a Builder Pattern for the array classes. When the builder is finalized, an appropriate implementation of the interface is choosen to fit the data. Here are some optimizations that are done upon build:

  • Special implementation for empty arrays
  • Small arrays are backed by an OnHeap array (regular long[], int[], etc)
  • Medium sized arrays are backed by a single DirectBuffer
  • Very large arrays (more than 2^26 elements) are backed by a number of direct buffers
  • If all values fit a smaller primitive, they will be warped (long to int, int to short etc)

Features

  • 64-bit indexing
  • Thread-safe (after build() has been called)
  • Immutability using a Builder pattern
  • Booleans are stored as efficient bitmaps
  • Backing structure is decided depending on the data
  • Allocated buffers are cleared as soon as they are no longer used (no need to wait for GC)

Supported Types

The following interfaces are part of the API:

  • BooleanImmutableArray
  • ByteImmutableArray
  • DoubleImmutableArray
  • FloatImmutableArray
  • IntImmutableArray
  • LongImmutableArray
  • ShortImmutableArray

Example

  1. LongImmutableArray array = LongImmutableArray.builder()
  2. .append(5)
  3. .append(100_232)
  4. .append(-32)
  5. .build();

If all values follow the same pattern, they can be compressed upon build.

  1. LongImmutableArray array = LongImmutableArray.builder()
  2. .append(1)
  3. .append(2)
  4. .append(3)
  5. .build(); // Will be backed internally by a `byte[]` of length 3

Usage

Add the following to your pom.xml-file. The library has no external dependencies.

  1. <dependency>
  2. <groupId>com.github.pyknic</groupId>
  3. <artifactId>immutable-array</artifactId>
  4. <version>1.0.2</version>
  5. </dependency>

License

Copyright 2016 Emil Forslund

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.