Android Open Source - Tanks Vector






From Project

Back to project page Tanks.

License

The source code is released under:

MIT License

If you think the Android project Tanks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ThirtyNineEighty.Helpers;
/*from www  .  j a  v  a  2  s  .c  o m*/
import android.util.Log;
import android.util.SparseArray;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public abstract class Vector
{
  //region nested type

  private static class Vectors
  {
    private ArrayDeque<Vector> vectors;
    private int size;

    public Vectors(int size)
    {
      vectors = new ArrayDeque<Vector>();
      this.size = size;
    }

    public void release(Vector vector)
    {
      if (vector.getSize() != size)
        return;

      vectors.add(vector);
    }

    public Vector get() { return vectors.poll(); }
    public int getCacheSize() { return vectors.size(); }
    public int getVectorSize() { return size; }
  }

  //endregion

  protected static final float epsilon = 0.0001f;

  // region cache

  private static final int cacheSizeLimit = 1000;
  private static final SparseArray<Vectors> cache = new SparseArray<Vectors>();
  private static final AtomicInteger cacheSize = new AtomicInteger();

  public static <TVector extends Vector> TVector getInstance(int vectorSize, TVector copy)
  {
    TVector vector = getInstance(vectorSize);

    for(int i = 0; i < vectorSize; i++)
    {
      float value = copy.get(i);
      vector.set(i, value);
    }

    return vector;
  }

  @SuppressWarnings({ "unchecked" })
  public static <TVector extends Vector> TVector getInstance(int vectorSize)
  {
    Vectors vectors;
    synchronized (cache)
    {
      vectors = cache.get(vectorSize);
      if (vectors == null)
        cache.append(vectorSize, vectors = new Vectors(vectorSize));
    }

    synchronized (vectors)
    {
      Vector vector = vectors.get();
      if (vector != null)
      {
        cacheSize.decrementAndGet();
        return (TVector) vector;
      }
    }

    try
    {
      switch (vectorSize)
      {
      case 2:
        return (TVector)new Vector2();
      case 3:
      case 4:
        return (TVector)new Vector3();
      default:
        return null;
      }
    }
    catch (Exception e)
    {
      Log.e("Vector", e.getMessage());
      return null;
    }
  }

  public static <TVector extends Vector> void release(Collection<TVector> vectors)
  {
    for(TVector vec : vectors)
      release(vec);
  }

  public static void release(Vector vector)
  {
    if (cacheSize.get() >= cacheSizeLimit)
      return;

    cacheSize.incrementAndGet();

    int size = vector.getSize();
    Vectors vectors;
    synchronized (cache)
    {
      vectors = cache.get(size);
      if (vectors == null)
        cache.append(size, vectors = new Vectors(size));
    }

    synchronized (vectors)
    {
      vectors.release(vector);
    }
  }

  public static String getCacheStatus()
  {
    StringBuilder builder = new StringBuilder();
    synchronized (cache)
    {
      int size = cache.size();
      for (int i = 0; i < size; i++)
      {
        int key = cache.keyAt(i);
        Vectors vectors = cache.get(key);

        builder.append(String.format("[Size: %s, VectorSize: %s] /n", vectors.getCacheSize(), vectors.getVectorSize()));
      }
    }

    return builder.toString();
  }

  //endregion

  public abstract float get(int num);
  public abstract void set(int num, float value);
  public abstract int getSize();
}




Java Source Code List

com.ThirtyNineEighty.Game.EngineObject.java
com.ThirtyNineEighty.Game.IEngineObject.java
com.ThirtyNineEighty.Game.Collisions.Collidable.java
com.ThirtyNineEighty.Game.Collisions.Collision2D.java
com.ThirtyNineEighty.Game.Collisions.Collision3D.java
com.ThirtyNineEighty.Game.Collisions.CollisionManager.java
com.ThirtyNineEighty.Game.Collisions.Collision.java
com.ThirtyNineEighty.Game.Collisions.ICollidable.java
com.ThirtyNineEighty.Game.Gameplay.Bullet.java
com.ThirtyNineEighty.Game.Gameplay.GameObject.java
com.ThirtyNineEighty.Game.Gameplay.Tank.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.CharacteristicFactory.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Characteristic.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Upgrade.java
com.ThirtyNineEighty.Game.Menu.BaseMenu.java
com.ThirtyNineEighty.Game.Menu.GameMenu.java
com.ThirtyNineEighty.Game.Menu.IMenu.java
com.ThirtyNineEighty.Game.Menu.Controls.Button.java
com.ThirtyNineEighty.Game.Menu.Controls.IControl.java
com.ThirtyNineEighty.Game.Worlds.GameWorld.java
com.ThirtyNineEighty.Game.Worlds.IWorld.java
com.ThirtyNineEighty.Helpers.Plane.java
com.ThirtyNineEighty.Helpers.Vector2.java
com.ThirtyNineEighty.Helpers.Vector3.java
com.ThirtyNineEighty.Helpers.VectorUtils.java
com.ThirtyNineEighty.Helpers.Vector.java
com.ThirtyNineEighty.Renderable.Renderable.java
com.ThirtyNineEighty.Renderable.Shader2D.java
com.ThirtyNineEighty.Renderable.Shader3D.java
com.ThirtyNineEighty.Renderable.Shader.java
com.ThirtyNineEighty.Renderable.Renderable2D.I2DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable2D.Label.java
com.ThirtyNineEighty.Renderable.Renderable2D.Sprite.java
com.ThirtyNineEighty.Renderable.Renderable3D.I3DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable3D.Model3D.java
com.ThirtyNineEighty.System.ConfigChooser.java
com.ThirtyNineEighty.System.Content.java
com.ThirtyNineEighty.System.GameActivity.java
com.ThirtyNineEighty.System.GameContext.java
com.ThirtyNineEighty.System.IContent.java
com.ThirtyNineEighty.System.ISubprogram.java