https://github.com/sunsided/android-utils
Android utility library with math voodoo and object caching
https://github.com/sunsided/android-utils
android caching java
Last synced: about 1 year ago
JSON representation
Android utility library with math voodoo and object caching
- Host: GitHub
- URL: https://github.com/sunsided/android-utils
- Owner: sunsided
- Created: 2012-03-05T21:44:14.000Z (over 14 years ago)
- Default Branch: development
- Last Pushed: 2012-03-05T21:56:37.000Z (over 14 years ago)
- Last Synced: 2025-03-31T07:34:25.282Z (over 1 year ago)
- Topics: android, caching, java
- Language: Java
- Homepage:
- Size: 474 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
# About this library
First of all, this is a work in progress. Second, it's a helper library for Android projects.
What this library intents to achieve is:
* Provide a foundation for multimedia Android projects, i.e. games
* Nothing more.
## The object cache
To avoid the problems that arise with rapid object creation and the garbage collection (for instance in the context of
scene graphs where rapid matrix and vector multiplication is needed) a caching mini-framework is provided.
It consists of a thread local object cache that creates objects on the fly if needed and recycles existing objects
where it can.
### Integrate the cache
Create a singleton field:
```Java
public static final IObjectCache Cache = new ThreadLocalObjectCache(new ObjectFactory() {
@NotNull @Override
public Matrix4 createNew() {
return new Matrix4();
}
});
```
and maybe some convenience methods:
```Java
@NotNull
public static Matrix4 createNew() {
Matrix4 matrix = Cache.getOrCreate(); // this instance may be uninitialized, i.e. dirty
return matrix.toUnit(); // clean it up
}
public static void recycle(@NotNull Matrix4 matrix) {
Cache.registerElement(matrix);
}
public void recycle() {
Cache.registerElement(this);
}
```
### Request an object from the cache
```Java
Matrix4 officialWay = Matrix4.Cache.getOrCreate();
Matrix4 convenientWay = Matrix4.createNew();
```
### When done, recycle the object
```Java
// official way
Matrix4.Cache.recycle(matrix);
// convenient way
matrix.recycle();
```
As soon as the object is recycled, it can be reused.