List of usage examples for org.apache.commons.lang StringUtils join
public static String join(Collection<?> collection, String separator)
Joins the elements of the provided Collection
into a single String containing the provided elements.
From source file:edu.indiana.d2i.sloan.internal.StopVMSimulator.java
public static void main(String[] args) { StopVMSimulator simulator = new StopVMSimulator(); CommandLineParser parser = new PosixParser(); try {/*from www . j ava 2 s .c om*/ CommandLine line = simulator.parseCommandLine(parser, args); String wdir = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR)); if (!HypervisorCmdSimulator.resourceExist(wdir)) { logger.error(String.format("Cannot find VM working dir: %s", wdir)); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_EXIST)); } Properties prop = new Properties(); String filename = HypervisorCmdSimulator.cleanPath(wdir) + HypervisorCmdSimulator.VM_INFO_FILE_NAME; prop.load(new FileInputStream(new File(filename))); // cannot stop VM when it is not running VMState currentState = VMState.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE))); if (!currentState.equals(VMState.RUNNING)) { logger.error("Cannot perform stop when VM is not running"); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_RUNNING)); } // stop VM try { Thread.sleep(1000); } catch (InterruptedException e) { logger.error(e.getMessage()); } // update VM status file, i.e. set state to shutdown prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE), VMState.SHUTDOWN.toString()); // save VM state file back prop.store(new FileOutputStream(new File(filename)), ""); // success System.exit(0); } catch (ParseException e) { logger.error(String.format("Cannot parse input arguments: %s%n, expected:%n%s", StringUtils.join(args, " "), simulator.getUsage(100, "", 5, 5, ""))); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_INPUT_ARGS)); } catch (IOException e) { logger.error(e.getMessage(), e); System.exit(ERROR_CODE.get(ERROR_STATE.IO_ERR)); } }
From source file:com.example.java.collections.ArrayExample.java
public static void main(String[] args) { /* ########################################################### */ // Initializing an Array String[] creatures = { "goldfish", "oscar", "guppy", "minnow" }; int[] numbers = new int[10]; int counter = 0; while (counter < numbers.length) { numbers[counter] = counter;// w w w.j av a 2 s . co m System.out.println("number[" + counter + "]: " + counter); counter++; } for (int theInt : numbers) { System.out.println(theInt); } //System.out.println(numbers[numbers.length]); /* ########################################################### */ /* ########################################################### */ // Using Charecter Array String name = "Michael"; // Charecter Array char[] charName = name.toCharArray(); System.out.println(charName); // Array Fuctions char[] html = new char[] { 'M', 'i', 'c', 'h', 'a', 'e', 'l' }; char[] lastFour = new char[4]; System.arraycopy(html, 3, lastFour, 0, lastFour.length); System.out.println(lastFour); /* ########################################################### */ /* ########################################################### */ // Using Arrays of Other Types Object[] person = new Object[] { "Michael", new Integer(94), new Integer(1), new Date() }; String fname = (String) person[0]; //ok Integer age = (Integer) person[1]; //ok Date start = (Date) person[2]; //oops! /* ########################################################### */ /* ########################################################### */ // Muti Dimestional Array String[][] bits = { { "Michael", "Ernest", "MFE" }, { "Ernest", "Friedman-Hill", "EFH" }, { "Kathi", "Duggan", "KD" }, { "Jeff", "Kellum", "JK" } }; bits[0] = new String[] { "Rudy", "Polanski", "RP" }; bits[1] = new String[] { "Rudy", "Washington", "RW" }; bits[2] = new String[] { "Rudy", "O'Reilly", "RO" }; /* ########################################################### */ /* ########################################################### */ //Create ArrayList from array String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray)); System.out.println(arrayList); // [a, b, c, d, e] /* ########################################################### */ /* ########################################################### */ //Check if an array contains a certain value String[] stringArray1 = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true /* ########################################################### */ /* ########################################################### */ //Concatenate two arrays int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang library int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); /* ########################################################### */ /* ########################################################### */ //Joins the elements of the provided array into a single String // Apache common lang String j = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); // a, b, c /* ########################################################### */ /* ########################################################### */ //Covnert ArrayList to Array String[] stringArray3 = { "a", "b", "c", "d", "e" }; ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList(stringArray)); String[] stringArr = new String[arrayList.size()]; arrayList.toArray(stringArr); for (String s : stringArr) { System.out.println(s); } /* ########################################################### */ /* ########################################################### */ //Convert Array to Set Set<String> set = new HashSet<String>(Arrays.asList(stringArray)); System.out.println(set); //[d, e, b, c, a] /* ########################################################### */ /* ########################################################### */ //Reverse an array int[] intArray1 = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray1); System.out.println(Arrays.toString(intArray1)); //[5, 4, 3, 2, 1] /* ########################################################### */ /* ########################################################### */ // Remove element of an array int[] intArray3 = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray3, 3);//create a new array System.out.println(Arrays.toString(removed)); /* ########################################################### */ /* ########################################################### */ byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) { System.out.format("0x%x ", t); } /* ########################################################### */ }
From source file:cn.dehui.zbj1984105.GetRelatedKeywords.java
public static void main(String[] args) { try {/*ww w . jav a2s. c om*/ // Log SOAP XML request and response. AdWordsServiceLogger.log(); // Get AdWordsUser from "~/adwords.properties". AdWordsUser user = new AdWordsUser("adwords.properties"); // Get the TargetingIdeaService. TargetingIdeaServiceInterface targetingIdeaService = user .getService(AdWordsService.V201209.TARGETING_IDEA_SERVICE); // Create selector. TargetingIdeaSelector selector = new TargetingIdeaSelector(); selector.setRequestType(RequestType.IDEAS); selector.setIdeaType(IdeaType.KEYWORD); selector.setRequestedAttributeTypes(new AttributeType[] { AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES }); // Set selector paging (required for targeting idea service). Paging paging = new Paging(); paging.setStartIndex(0); paging.setNumberResults(10); selector.setPaging(paging); // Create related to query search parameter. RelatedToQuerySearchParameter relatedToQuerySearchParameter = new RelatedToQuerySearchParameter(); relatedToQuerySearchParameter.setQueries(new String[] { "mars cruise" }); selector.setSearchParameters(new SearchParameter[] { relatedToQuerySearchParameter }); // Get related keywords. TargetingIdeaPage page = targetingIdeaService.get(selector); // Display related keywords. if (page.getEntries() != null && page.getEntries().length > 0) { for (TargetingIdea targetingIdea : page.getEntries()) { Map<AttributeType, Attribute> data = MapUtils.toMap(targetingIdea.getData()); StringAttribute keyword = (StringAttribute) data.get(AttributeType.KEYWORD_TEXT); IntegerSetAttribute categories = (IntegerSetAttribute) data .get(AttributeType.CATEGORY_PRODUCTS_AND_SERVICES); String categoriesString = "(none)"; if (categories != null && categories.getValue() != null) { categoriesString = StringUtils.join(ArrayUtils.toObject(categories.getValue()), ", "); } Long averageMonthlySearches = ((LongAttribute) data.get(AttributeType.SEARCH_VOLUME)) .getValue(); System.out.println( "Keyword with text '" + keyword.getValue() + "' and average monthly search volume '" + averageMonthlySearches + "' was found with categories: " + categoriesString); } } else { System.out.println("No related keywords were found."); } } catch (Exception e) { e.printStackTrace(); } }
From source file:de.escidoc.core.admin.AdminMain.java
/** * Main Method, depends on args[0] which method is executed. * //from w w w . j a v a 2 s .co m * @param args * arguments given on commandline * @throws NoSuchMethodException * e * @throws InvocationTargetException * e * @throws IllegalAccessException * e */ public static void main(final String[] args) { int result = 20; try { AdminMain admin = new AdminMain(); // call has to have at least one argument if (args.length > 0 && !StringUtils.isEmpty(args[0])) { String methodToCall = methods.get(args[0]); if (!StringUtils.isEmpty(methodToCall)) { log.info("memory (free / max. available): " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + " MB" + " / " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " MB"); Class<?>[] paramTypes = { String[].class }; Method thisMethod = admin.getClass().getDeclaredMethod(methodToCall, paramTypes); thisMethod.invoke(admin, new Object[] { args }); result = 0; } else { admin.failMessage("provided tool name: " + StringUtils.join(args, " ")); } } else { admin.failMessage(); } } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); } System.exit(result); }
From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step5LinguisticPreprocessing.java
public static void main(String[] args) throws Exception { // input dir - list of xml query containers // step4-boiler-plate/ File inputDir = new File(args[0]); // output dir File outputDir = new File(args[1]); if (!outputDir.exists()) { outputDir.mkdirs();/*from w w w .j a v a2 s. c o m*/ } // iterate over query containers for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) { QueryResultContainer queryResultContainer = QueryResultContainer .fromXML(FileUtils.readFileToString(f, "utf-8")); for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) { // System.out.println(rankedResults.plainText); if (rankedResults.plainText != null) { String[] lines = StringUtils.split(rankedResults.plainText, "\n"); // collecting all cleaned lines List<String> cleanLines = new ArrayList<>(lines.length); // collecting line tags List<String> lineTags = new ArrayList<>(lines.length); for (String line : lines) { // get the tag String tag = null; Matcher m = OPENING_TAG_PATTERN.matcher(line); if (m.find()) { tag = m.group(1); } if (tag == null) { throw new IllegalArgumentException("No html tag found for line:\n" + line); } // replace the tag at the beginning and the end String noTagText = line.replaceAll("^<\\S+>", "").replaceAll("</\\S+>$", ""); // do some html cleaning noTagText = noTagText.replaceAll(" ", " "); noTagText = noTagText.trim(); // add to the output if (!noTagText.isEmpty()) { cleanLines.add(noTagText); lineTags.add(tag); } } if (cleanLines.isEmpty()) { // the document is empty System.err.println("Document " + rankedResults.clueWebID + " in query " + queryResultContainer.qID + " is empty"); } else { // now join them back to paragraphs String text = StringUtils.join(cleanLines, "\n"); // create JCas JCas jCas = JCasFactory.createJCas(); jCas.setDocumentText(text); jCas.setDocumentLanguage("en"); // annotate WebParagraph SimplePipeline.runPipeline(jCas, AnalysisEngineFactory.createEngineDescription(WebParagraphAnnotator.class)); // fill the original tag information List<WebParagraph> webParagraphs = new ArrayList<>( JCasUtil.select(jCas, WebParagraph.class)); // they must be the same size as original ones if (webParagraphs.size() != lineTags.size()) { throw new IllegalStateException( "Different size of annotated paragraphs and original lines"); } for (int i = 0; i < webParagraphs.size(); i++) { WebParagraph p = webParagraphs.get(i); // get tag String tag = lineTags.get(i); p.setOriginalHtmlTag(tag); } SimplePipeline.runPipeline(jCas, AnalysisEngineFactory.createEngineDescription(StanfordSegmenter.class, // only on existing WebParagraph annotations StanfordSegmenter.PARAM_ZONE_TYPES, WebParagraph.class.getCanonicalName())); // now convert to XMI ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); XmiCasSerializer.serialize(jCas.getCas(), byteOutputStream); // encode to base64 String encoded = new BASE64Encoder().encode(byteOutputStream.toByteArray()); rankedResults.originalXmi = encoded; } } } // and save the query to output dir File outputFile = new File(outputDir, queryResultContainer.qID + ".xml"); FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8"); System.out.println("Finished " + outputFile); } }
From source file:edu.indiana.d2i.sloan.internal.LaunchVMSimulator.java
/** * @param args/*from ww w . j a v a2 s . com*/ */ public static void main(String[] args) { LaunchVMSimulator simulator = new LaunchVMSimulator(); CommandLineParser parser = new PosixParser(); try { CommandLine line = simulator.parseCommandLine(parser, args); String wdir = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR)); String mode = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE)); String policyFilePath = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.POLICY_PATH)); if (!HypervisorCmdSimulator.resourceExist(wdir)) { logger.error(String.format("Cannot find VM working dir: %s", wdir)); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_EXIST)); } VMMode vmmode = HypervisorCmdSimulator.getVMMode(mode); if (vmmode == null) { logger.error(String.format("Invalid requested mode: %s, can only be %s or %s", mode, VMMode.MAINTENANCE.toString(), VMMode.SECURE.toString())); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_VM_MODE)); } if (!HypervisorCmdSimulator.resourceExist(policyFilePath)) { logger.error(String.format("Cannot find plicy file: %s", policyFilePath)); System.exit(ERROR_CODE.get(ERROR_STATE.FIREWALL_POLICY_NOT_EXIST)); } // load VM status info Properties prop = new Properties(); String filename = HypervisorCmdSimulator.cleanPath(wdir) + HypervisorCmdSimulator.VM_INFO_FILE_NAME; prop.load(new FileInputStream(new File(filename))); // can only launch VM when it is in shutdown state VMState currentState = VMState.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE))); if (!currentState.equals(VMState.SHUTDOWN)) { logger.error(String.format("Can only launch VM when it is in %s state, current VM state is %s", VMState.SHUTDOWN.toString(), currentState.toString())); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_SHUTDOWN)); } // launch VM try { Thread.sleep(1000); } catch (InterruptedException e) { logger.error(e.getMessage()); } // update VM state file // set following properties prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.POLICY_PATH), policyFilePath); prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE), vmmode.toString()); // set VM state to running prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE), VMState.RUNNING.toString()); // save VM state file back prop.store(new FileOutputStream(new File(filename)), ""); // success System.exit(0); } catch (ParseException e) { logger.error(String.format("Cannot parse input arguments: %s%n, expected:%n%s", StringUtils.join(args, " "), simulator.getUsage(100, "", 5, 5, ""))); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_INPUT_ARGS)); } catch (FileNotFoundException e) { logger.error(String.format("Cannot find vm state file: %s", e.getMessage())); System.exit(ERROR_CODE.get(ERROR_STATE.VM_STATE_FILE_NOT_FOUND)); } catch (IOException e) { logger.error(e.getMessage(), e); System.exit(ERROR_CODE.get(ERROR_STATE.IO_ERR)); } }
From source file:edu.indiana.d2i.sloan.internal.QueryVMSimulator.java
public static void main(String[] args) { QueryVMSimulator simulator = new QueryVMSimulator(); CommandLineParser parser = new PosixParser(); try {// w ww .j ava 2 s. c o m CommandLine line = simulator.parseCommandLine(parser, args); String wdir = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR)); if (!HypervisorCmdSimulator.resourceExist(wdir)) { logger.error(String.format("Cannot find VM working dir: %s", wdir)); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_EXIST)); } Properties prop = new Properties(); String filename = HypervisorCmdSimulator.cleanPath(wdir) + HypervisorCmdSimulator.VM_INFO_FILE_NAME; prop.load(new FileInputStream(new File(filename))); VMStatus status = new VMStatus( VMMode.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE))), VMState.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE))), ip, Integer.parseInt(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VNC_PORT))), Integer.parseInt(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.SSH_PORT))), Integer.parseInt(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VCPU))), Integer.parseInt(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.MEM)))); // write state query file so shell script can read the vm state info filename = HypervisorCmdSimulator.cleanPath(wdir) + QueryVMSimulator.QUERY_RES_FILE_NAME; FileUtils.writeStringToFile(new File(filename), status.toString(), Charset.forName("UTF-8")); } catch (ParseException e) { logger.error(String.format("Cannot parse input arguments: %s%n, expected:%n%s", StringUtils.join(args, " "), simulator.getUsage(100, "", 5, 5, ""))); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_INPUT_ARGS)); } catch (IOException e) { logger.error(e.getMessage(), e); System.exit(ERROR_CODE.get(ERROR_STATE.IO_ERR)); } }
From source file:edu.indiana.d2i.sloan.internal.SwitchVMSimulator.java
/** * @param args/*from ww w.j a v a 2s . c om*/ */ public static void main(String[] args) { SwitchVMSimulator simulator = new SwitchVMSimulator(); CommandLineParser parser = new PosixParser(); try { CommandLine line = simulator.parseCommandLine(parser, args); String wdir = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR)); String mode = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE)); String policyFilePath = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.POLICY_PATH)); if (!HypervisorCmdSimulator.resourceExist(wdir)) { logger.error(String.format("Cannot find VM working dir: %s", wdir)); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_EXIST)); } VMMode requestedMode = HypervisorCmdSimulator.getVMMode(mode); if (requestedMode == null) { logger.error(String.format("Invalid requested mode: %s, can only be %s or %s", mode, VMMode.MAINTENANCE.toString(), VMMode.SECURE.toString())); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_VM_MODE)); } if (!HypervisorCmdSimulator.resourceExist(policyFilePath)) { logger.error(String.format("Cannot find plicy file: %s", policyFilePath)); System.exit(ERROR_CODE.get(ERROR_STATE.FIREWALL_POLICY_NOT_EXIST)); } // load VM state file Properties prop = new Properties(); String filename = HypervisorCmdSimulator.cleanPath(wdir) + HypervisorCmdSimulator.VM_INFO_FILE_NAME; prop.load(new FileInputStream(new File(filename))); // cannot switch when VM is not running VMState currentState = VMState.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE))); if (!currentState.equals(VMState.RUNNING)) { logger.error("Cannot perform switch when VM is not running"); System.exit(ERROR_CODE.get(ERROR_STATE.VM_NOT_RUNNING)); } // get current mode VMMode currentMode = VMMode.valueOf(prop.getProperty(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE))); if (currentMode.equals(requestedMode)) { logger.error(String.format("VM is already in the requested mode: %s", requestedMode.toString())); System.exit(ERROR_CODE.get(ERROR_STATE.VM_ALREADY_IN_REQUESTED_MODE)); } // switch VM try { Thread.sleep(1000); } catch (InterruptedException e) { logger.error(e.getMessage()); } // update firewall policy prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.POLICY_PATH), policyFilePath); // update VM status file, i.e. set mode to the requested mode prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE), requestedMode.toString()); // save VM state file back prop.store(new FileOutputStream(new File(filename)), ""); // success System.exit(0); } catch (ParseException e) { logger.error(String.format("Cannot parse input arguments: %s%n, expected:%n%s", StringUtils.join(args, " "), simulator.getUsage(100, "", 5, 5, ""))); System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_INPUT_ARGS)); } catch (FileNotFoundException e) { logger.error(String.format("Cannot find vm state file: %s", e.getMessage())); System.exit(ERROR_CODE.get(ERROR_STATE.VM_STATE_FILE_NOT_FOUND)); } catch (IOException e) { logger.error(e.getMessage(), e); System.exit(ERROR_CODE.get(ERROR_STATE.IO_ERR)); } }
From source file:com.mmj.app.common.core.lang.ArrayUtils.java
public static void main(String[] args) { String[] s = { "zxc", null, "", "" }; s = ArrayUtils.replaceNullElement(s, new IHandle<String>() { @Override/*from w w w.ja va 2 s . com*/ public boolean isNull(String obj) { return obj == null || StringUtils.isEmpty((String) obj); } @Override public String init(Class<String> clazz) { if (clazz.equals(String.class)) { return (String) new String("wu"); } try { return clazz.newInstance(); } catch (InstantiationException e) { System.out.println(e.getMessage()); } catch (IllegalAccessException e) { System.out.println(e.getMessage()); } System.out.println("replaceNullElement: init error"); return null; } }); System.out.println(StringUtils.join(s, ";")); String[] a = removeBlankElement(new String[] { "1", null, "2", "", "3" }); for (int i = 0, j = a.length; i < j; i++) { System.out.println(a[i]); } String[] t = null; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); t = new String[] { "1", null, "2", "", "3" }; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); }
From source file:com.zb.app.common.core.lang.ArrayUtils.java
public static void main(String[] args) { String[] s = { "zxc", null, "zuobian", "" }; s = ArrayUtils.replaceNullElement(s, new IHandle<String>() { @Override//from ww w . j a v a 2 s .c o m public boolean isNull(String obj) { return obj == null || StringUtils.isEmpty((String) obj); } @Override public String init(Class<String> clazz) { if (clazz.equals(String.class)) { return (String) new String("wu"); } try { return clazz.newInstance(); } catch (InstantiationException e) { System.out.println(e.getMessage()); } catch (IllegalAccessException e) { System.out.println(e.getMessage()); } System.out.println("replaceNullElement: init error"); return null; } }); System.out.println(StringUtils.join(s, ";")); String[] a = removeBlankElement(new String[] { "1", null, "2", "", "3" }); for (int i = 0, j = a.length; i < j; i++) { System.out.println(a[i]); } String[] t = null; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); t = new String[] { "1", null, "2", "", "3" }; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); }