Method: concat(Object[], Object[])

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.utils;
23:
24: import java.io.BufferedReader;
25: import java.io.File;
26: import java.io.FileInputStream;
27: import java.io.FileNotFoundException;
28: import java.io.IOException;
29: import java.io.InputStream;
30: import java.io.InputStreamReader;
31: import java.io.UnsupportedEncodingException;
32: import java.util.ArrayList;
33: import java.util.Arrays;
34: import java.util.LinkedList;
35: import java.util.List;
36:
37: public class GeneralUtils
38: {
39:         public static boolean isEscapeSequence(char c)
40:         {
41:                 return c == '\t' || c == '\b' || c == '\n' || c == '\r' || c == '\f'
42:                                 || c == '\'' || c == '\"' || c == '\\';
43:         }
44:
45:         public static StringBuffer readFromFile(String relativepath,
46:                         Class<?> classRef) throws IOException
47:         {
48:                 InputStream input = classRef.getResourceAsStream('/'
49:                                 + relativepath.replace("\\", "/"));
50:
51:                 return readFromInputStream(input);
52:         }
53:
54:         public static StringBuffer readFromInputStream(InputStream input)
55:                         throws IOException
56:         {
57:                 if (input == null)
58:                 {
59:                         return null;
60:                 }
61:
62:                 StringBuffer data = new StringBuffer();
63:                 int c = 0;
64:                 while ((c = input.read()) != -1)
65:                 {
66:                         data.append((char) c);
67:                 }
68:                 input.close();
69:
70:                 return data;
71:         }
72:
73:         public static String readFromFile(File file) throws IOException
74:         {
75:                 return readLines(file, System.getProperty("line.separator")).toString().trim();
76:         }
77:
78:         public static StringBuilder readLines(File file, String outputLineEnding)
79:                         throws UnsupportedEncodingException, FileNotFoundException,
80:                         IOException
81:         {
82:                 StringBuilder data = new StringBuilder();
83:                 BufferedReader in = null;
84:
85:                 try
86:                 {
87:                         in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
88:
89:                         String str = "";
90:
91:                         while ((str = in.readLine()) != null)
92:                         {
93:                                 data.append(str + outputLineEnding);
94:                         }
95:
96:                 } finally
97:                 {
98:                         if (in != null)
99:                         {
100:                                 in.close();
101:                         }
102:                 }
103:
104:                 return data;
105:         }
106:
107:         public static List<File> getFiles(File folder)
108:         {
109:                 File[] listOfFiles = folder.listFiles();
110:
111:                 List<File> fileList = new LinkedList<File>();
112:
113:                 if (listOfFiles == null || listOfFiles.length == 0)
114:                 {
115:                         return fileList;
116:                 }
117:
118:                 for (File file : listOfFiles)
119:                 {
120:                         if (file.isFile())
121:                         {
122:                                 fileList.add(file);
123:                         }
124:                 }
125:
126:                 return fileList;
127:         }
128:
129:         public static void deleteFolderContents(File folder, boolean removeFolders)
130:         {
131:                 deleteFolderContents(folder, new ArrayList<String>(), removeFolders);
132:         }
133:
134:         public static void deleteFolderContents(File folder,
135:                         List<String> folderNamesToAvoid, boolean removeFolders)
136:         {
137:                 if (folder == null)
138:                 {
139:                         return;
140:                 }
141:
142:                 File[] files = folder.listFiles();
143:
144:                 if (files == null)
145:                 {
146:                         return;
147:                 }
148:
149:                 for (File f : files)
150:                 {
151:                         if (f.isDirectory())
152:                         {
153:                                 if (!folderNamesToAvoid.contains(f.getName()))
154:                                 {
155:                                         deleteFolderContents(f, folderNamesToAvoid, removeFolders);
156:                                 }
157:                         } else
158:                         {
159:                                 f.delete();
160:                         }
161:
162:                         if (removeFolders)
163:                         {
164:                                 f.delete();
165:                         }
166:                 }
167:         }
168:
169:         public static <T> T[] concat(T[] first, T[] second)
170:         {
171:                 T[] result = Arrays.copyOf(first, first.length + second.length);
172:                 System.arraycopy(second, 0, result, first.length, second.length);
173:                 return result;
174:         }
175:
176:         public static String cleanupWhiteSpaces(String str)
177:         {
178:                 return str.replaceAll("\\s+", " ").trim();
179:         }
180:
181:         public static List<File> getFilesRecursively(File folder)
182:         {
183:                 File[] listOfFiles = folder.listFiles();
184:
185:                 List<File> fileList = new LinkedList<File>();
186:
187:                 if (listOfFiles == null || listOfFiles.length == 0)
188:                 {
189:                         return fileList;
190:                 }
191:
192:                 for (File file : listOfFiles)
193:                 {
194:                         if (file.isFile())
195:                         {
196:                                 fileList.add(file);
197:                         }
198:                         if (file.isDirectory())
199:                         {
200:                                 fileList.addAll(getFilesRecursively(file));
201:                         }
202:                 }
203:                 return fileList;
204:         }
205: }