001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.util.array; 018 019import java.util.Arrays; 020 021/** 022 * Utility class for writing containers to arrays. 023 * 024 * It is generally preferable to use the static methods this class offers than 025 * using {@link ArrayWritable#writeToArray(int, Object[], int, int)} directly. 026 * 027 * @author Malte Isberner <malte.isberner@gmail.com> 028 * 029 */ 030public abstract class AWUtil { 031 032 /* 033 * Prevent inheritance. 034 */ 035 private AWUtil() { 036 } 037 038 039 /** 040 * Writes the complete container data to an array. This method ensures that the array's capacity 041 * is not exceeded. 042 * @param aw the container. 043 * @param array the array 044 * @return the number of elements copied 045 */ 046 public static <T,U extends T> int safeWrite(ArrayWritable<U> aw, T[] array) { 047 int num = aw.size(); 048 if(num <= 0) 049 return 0; 050 if(num > array.length) 051 num = array.length; 052 aw.writeToArray(0, array, 0, num); 053 return num; 054 } 055 056 /** 057 * Writes a given maximum amount of data items from a container to an array. This 058 * method ensures that the array's capacity is not exceeded. 059 * @param num the number of elements to copy 060 * @param aw the container. 061 * @param array the array 062 * @return the number of elements copied 063 */ 064 public static <T,U extends T> int safeWrite(int num, ArrayWritable<U> aw, T[] array) { 065 int tmp = aw.size(); 066 if(tmp < num) 067 num = tmp; 068 tmp = array.length; 069 if(tmp < num) 070 num = tmp; 071 if(num <= 0) 072 return 0; 073 aw.writeToArray(0, array, 0, num); 074 return num; 075 } 076 077 public static <T, U extends T> int safeWrite(int num, ArrayWritable<U> aw, int ofs, T[] array, int tgtOfs) { 078 int tmp = aw.size() - ofs; 079 if(tmp < num) 080 num = tmp; 081 tmp = array.length - tgtOfs; 082 if(tmp < num) 083 num = tmp; 084 if(num <= 0) 085 return 0; 086 aw.writeToArray(ofs, array, tgtOfs, num); 087 return num; 088 } 089 090 public static <T, U extends T> int safeWrite(ArrayWritable<U> aw, T[] array, int tgtOfs) { 091 int num = array.length - tgtOfs; 092 int s = aw.size(); 093 if(s < num) 094 num = s; 095 if(num <= 0) 096 return 0; 097 aw.writeToArray(0, array, tgtOfs, num); 098 return num; 099 } 100 101 102 public static Object[] toArray(ArrayWritable<?> aw) { 103 int num = aw.size(); 104 Object[] arr = new Object[num]; 105 aw.writeToArray(0, arr, 0, num); 106 return arr; 107 } 108 109 public static <T> T[] toArray(ArrayWritable<?> aw, T[] arr) { 110 int num = aw.size(); 111 if(arr.length < num) 112 arr = Arrays.copyOf(arr, num); 113 aw.writeToArray(0, arr, 0, num); 114 return arr; 115 } 116}