Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.common;
     18 
     19 import javax.annotation.Nonnull;
     20 
     21 public final class CoordinateUtils {
     22     private static final int INDEX_X = 0;
     23     private static final int INDEX_Y = 1;
     24     private static final int ELEMENT_SIZE = INDEX_Y + 1;
     25 
     26     private CoordinateUtils() {
     27         // This utility class is not publicly instantiable.
     28     }
     29 
     30     @Nonnull
     31     public static int[] newInstance() {
     32         return new int[ELEMENT_SIZE];
     33     }
     34 
     35     public static int x(@Nonnull final int[] coords) {
     36         return coords[INDEX_X];
     37     }
     38 
     39     public static int y(@Nonnull final int[] coords) {
     40         return coords[INDEX_Y];
     41     }
     42 
     43     public static void set(@Nonnull final int[] coords, final int x, final int y) {
     44         coords[INDEX_X] = x;
     45         coords[INDEX_Y] = y;
     46     }
     47 
     48     public static void copy(@Nonnull final int[] destination, @Nonnull final int[] source) {
     49         destination[INDEX_X] = source[INDEX_X];
     50         destination[INDEX_Y] = source[INDEX_Y];
     51     }
     52 
     53     @Nonnull
     54     public static int[] newCoordinateArray(final int arraySize) {
     55         return new int[ELEMENT_SIZE * arraySize];
     56     }
     57 
     58     @Nonnull
     59     public static int[] newCoordinateArray(final int arraySize,
     60             final int defaultX, final int defaultY) {
     61         final int[] result = new int[ELEMENT_SIZE * arraySize];
     62         for (int i = 0; i < arraySize; ++i) {
     63             setXYInArray(result, i, defaultX, defaultY);
     64         }
     65         return result;
     66     }
     67 
     68     public static int xFromArray(@Nonnull final int[] coordsArray, final int index) {
     69         return coordsArray[ELEMENT_SIZE * index + INDEX_X];
     70     }
     71 
     72     public static int yFromArray(@Nonnull final int[] coordsArray, final int index) {
     73         return coordsArray[ELEMENT_SIZE * index + INDEX_Y];
     74     }
     75 
     76     @Nonnull
     77     public static int[] coordinateFromArray(@Nonnull final int[] coordsArray, final int index) {
     78         final int[] coords = newInstance();
     79         set(coords, xFromArray(coordsArray, index), yFromArray(coordsArray, index));
     80         return coords;
     81     }
     82 
     83     public static void setXYInArray(@Nonnull final int[] coordsArray, final int index,
     84             final int x, final int y) {
     85         final int baseIndex = ELEMENT_SIZE * index;
     86         coordsArray[baseIndex + INDEX_X] = x;
     87         coordsArray[baseIndex + INDEX_Y] = y;
     88     }
     89 
     90     public static void setCoordinateInArray(@Nonnull final int[] coordsArray, final int index,
     91             @Nonnull final int[] coords) {
     92         setXYInArray(coordsArray, index, x(coords), y(coords));
     93     }
     94 }
     95