List of usage examples for org.apache.commons.lang StringUtils countMatches
public static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
From source file:org.apache.sysml.runtime.io.FrameReaderTextCSV.java
/** * //from w w w. jav a2 s. c o m * @param files * @param fs * @param schema * @param names * @param hasHeader * @param delim * @return * @throws IOException */ protected Pair<Integer, Integer> computeCSVSize(Path path, JobConf job, FileSystem fs) throws IOException { TextInputFormat informat = new TextInputFormat(); informat.configure(job); InputSplit[] splits = informat.getSplits(job, 1); splits = IOUtilFunctions.sortInputSplits(splits); boolean first = true; int ncol = -1; int nrow = -1; for (InputSplit split : splits) { RecordReader<LongWritable, Text> reader = informat.getRecordReader(split, job, Reporter.NULL); LongWritable key = new LongWritable(); Text value = new Text(); try { //read head and first line to determine num columns if (first) { if (_props.hasHeader()) reader.next(key, value); //ignore header reader.next(key, value); ncol = StringUtils.countMatches(value.toString(), _props.getDelim()) + 1; nrow = 1; first = false; } //count remaining number of rows while (reader.next(key, value)) nrow++; } finally { IOUtilFunctions.closeSilently(reader); } } return new Pair<Integer, Integer>(nrow, ncol); }
From source file:org.apache.sysml.runtime.io.FrameReaderTextCSVParallel.java
@Override protected Pair<Integer, Integer> computeCSVSize(Path path, JobConf job, FileSystem fs) throws IOException { int numThreads = OptimizerUtils.getParallelTextReadParallelism(); TextInputFormat informat = new TextInputFormat(); informat.configure(job);/* w ww . j av a2s. c o m*/ InputSplit[] splits = informat.getSplits(job, numThreads); //compute number of columns RecordReader<LongWritable, Text> reader = informat.getRecordReader(splits[0], job, Reporter.NULL); LongWritable key = new LongWritable(); Text value = new Text(); reader.next(key, value); int ncol = StringUtils.countMatches(value.toString(), _props.getDelim()) + 1; reader.close(); //compute number of rows ExecutorService pool = Executors.newFixedThreadPool(numThreads); //compute num rows per split int nrow = 0; try { ArrayList<CountRowsTask> tasks = new ArrayList<CountRowsTask>(); for (int i = 0; i < splits.length; i++) tasks.add(new CountRowsTask(splits[i], informat, job, _props.hasHeader(), i == 0)); List<Future<Long>> cret = pool.invokeAll(tasks); for (Future<Long> count : cret) nrow += count.get().intValue(); } catch (Exception e) { throw new IOException("Failed parallel read of text csv input.", e); } return new Pair<Integer, Integer>(nrow, ncol); }
From source file:org.apache.sysml.runtime.io.ReaderTextCSV.java
private MatrixBlock computeCSVSize(List<Path> files, JobConf job, FileSystem fs, boolean hasHeader, String delim, boolean fill, double fillValue) throws IOException { int nrow = -1; int ncol = -1; String value = null;//from ww w. j av a2s.c o m String cellStr = null; for (int fileNo = 0; fileNo < files.size(); fileNo++) { BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(files.get(fileNo)))); try { // Read the header line, if there is one. if (fileNo == 0) { if (hasHeader) br.readLine(); //ignore header if ((value = br.readLine()) != null) { cellStr = value.toString().trim(); ncol = StringUtils.countMatches(cellStr, delim) + 1; nrow = 1; } } while ((value = br.readLine()) != null) { nrow++; } } finally { IOUtilFunctions.closeSilently(br); } } //create new matrix block (assume sparse for consistency w/ compiler) return new MatrixBlock(nrow, ncol, true); }
From source file:org.apache.sysml.runtime.io.ReaderTextCSVParallel.java
private MatrixBlock computeCSVSizeAndCreateOutputMatrixBlock(InputSplit[] splits, Path path, JobConf job, boolean hasHeader, String delim, long estnnz) throws IOException, DMLRuntimeException { int nrow = 0; int ncol = 0; FileInputFormat.addInputPath(job, path); TextInputFormat informat = new TextInputFormat(); informat.configure(job);// w ww.j a v a 2 s. c om // count no of entities in the first non-header row LongWritable key = new LongWritable(); Text oneLine = new Text(); RecordReader<LongWritable, Text> reader = informat.getRecordReader(splits[0], job, Reporter.NULL); try { if (reader.next(key, oneLine)) { String cellStr = oneLine.toString().trim(); ncol = StringUtils.countMatches(cellStr, delim) + 1; } } finally { IOUtilFunctions.closeSilently(reader); } // count rows in parallel per split try { ExecutorService pool = Executors.newFixedThreadPool(_numThreads); ArrayList<CountRowsTask> tasks = new ArrayList<CountRowsTask>(); for (InputSplit split : splits) { tasks.add(new CountRowsTask(split, informat, job, hasHeader)); hasHeader = false; } pool.invokeAll(tasks); pool.shutdown(); // collect row counts for offset computation // early error notify in case not all tasks successful _offsets = new SplitOffsetInfos(tasks.size()); for (CountRowsTask rt : tasks) { if (!rt.getReturnCode()) throw new IOException("Count task for csv input failed: " + rt.getErrMsg()); _offsets.setOffsetPerSplit(tasks.indexOf(rt), nrow); _offsets.setLenghtPerSplit(tasks.indexOf(rt), rt.getRowCount()); nrow = nrow + rt.getRowCount(); } } catch (Exception e) { throw new IOException("Threadpool Error " + e.getMessage(), e); } // allocate target matrix block based on given size; // need to allocate sparse as well since lock-free insert into target return createOutputMatrixBlock(nrow, ncol, nrow, ncol, estnnz, true, true); }
From source file:org.apache.torque.generator.configuration.controller.Output.java
/** * Returns the line break character(s) for this Output. * If the line break was already determined, the already determined * line break character(s) are returned, and content is ignored. * If the line break character was not already determined, * the occurence of the different line break characters is counted * and the largest is returned ("\r\n" for equal count) * If one of them is > 0, the result is cached and stored; * if all occurences are 0, the result is not cached and will be determined * anew if the method is called another time for the same output. * * @param content the already produced content. * @return the line break character(s), one of "\r", "\n", "\r\n". *//*from w w w .ja v a 2 s . co m*/ public String getOrDetermineLineBreak(String content) { if (lineBreak != null) { return lineBreak; } String contentString = content.toString(); int r = StringUtils.countMatches(contentString, CARRIAGE_RETURN); int n = StringUtils.countMatches(contentString, LINE_FEED); int rn = StringUtils.countMatches(contentString, CARRIAGE_RETURN_LINE_FEED); // r and n are contained within rn if ((rn >= r - rn) || (rn >= n - rn)) { if (rn > 0) { this.lineBreak = CARRIAGE_RETURN_LINE_FEED; } return CARRIAGE_RETURN_LINE_FEED; } else if (n > r) { this.lineBreak = LINE_FEED; return LINE_FEED; } else { this.lineBreak = CARRIAGE_RETURN; return CARRIAGE_RETURN; } }
From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java
@Test public void testAddRequestWithDelayedResponses() { HttpServletRequest httpServletRequest = mocksControl.createMock(HttpServletRequest.class); AsyncContext asyncContext = mocksControl.createMock(AsyncContext.class); expect(httpServletRequest.startAsync()).andReturn(asyncContext).atLeastOnce(); expect(httpServletRequest.getAsyncContext()).andReturn(asyncContext).atLeastOnce(); asyncContext.setTimeout(anyLong());/*from w w w . j a v a 2s . c o m*/ httpServletRequest.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>notNull()); asyncContext.addListener(EasyMock.<AsyncListener>anyObject()); asyncContext.dispatch(); expectLastCall().atLeastOnce(); Stanza body = BoshStanzaUtils.createBoshStanzaBuilder().startInnerElement("presence").endInnerElement() .build(); Capture<BoshResponse> captured = new Capture<BoshResponse>(); httpServletRequest.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE), EasyMock.<BoshResponse>capture(captured)); mocksControl.replay(); BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null, inactivityChecker); boshBackedSessionContext.writeBoshResponse(body); // queued for merging boshBackedSessionContext.writeBoshResponse(body); // queued for merging boshBackedSessionContext.writeBoshResponse(body); // queued for merging boshBackedSessionContext.insertRequest(new BoshRequest(httpServletRequest, body, 1L)); mocksControl.verify(); final String mergedAllBodiesStanza = new String(captured.getValue().getContent()); assertEquals(3, StringUtils.countMatches(mergedAllBodiesStanza, "<presence")); }
From source file:org.artifactory.storage.db.fs.util.NodeUtils.java
/** * Returns the number of path elements of this path. * Empty path returns 0, root path returns 1. * * @param path The path to check/*from w w w.j av a 2 s. co m*/ * @return Number of path elements in this path */ public static int getDepth(String path) { if (StringUtils.isBlank(path)) { return 0; } else { return StringUtils.countMatches(PathUtils.trimSlashes(path).toString(), "/") + 1; } }
From source file:org.artifactory.storage.db.util.JdbcHelper.java
public static String resolveQuery(String sql, Object[] params) { int expectedParamsCount = StringUtils.countMatches(sql, "?"); int paramsLength = params == null ? 0 : params.length; if (expectedParamsCount != paramsLength) { log.warn("Unexpected parameters count. Expected {} but got {}", expectedParamsCount, paramsLength); }/*www. j a v a2 s. c o m*/ if (params == null || params.length == 0) { return sql; } else if (expectedParamsCount == paramsLength) { // replace placeholders in the query with matching parameter values // this method doesn't attempt to escape characters Object[] printParams = new Object[params.length]; for (int i = 0; i < params.length; i++) { Object current = params[i]; if (current == null) { current = "NULL"; } else if (current instanceof String) { current = "'" + params[i] + "'"; } else if (current instanceof BlobWrapper) { current = "BLOB(length: " + ((BlobWrapper) params[i]).getLength() + ")"; } printParams[i] = current; } sql = sql.replaceAll("\\?", "%s"); sql = String.format(sql, printParams); } else { // params not null but there's a difference between actual and expected StringBuilder builder = new StringBuilder(); builder.append("Executing SQL: '").append(sql).append("'"); if (params.length > 0) { builder.append(" with params: "); for (int i = 0; i < params.length; i++) { builder.append("'"); builder.append(params[i]); builder.append("'"); if (i < params.length - 1) { builder.append(", "); } } } sql = builder.toString(); } return sql; }
From source file:org.atomserver.core.dbstore.CRUDDBSTest.java
private void selectAndCheckDeletion(String url) { ClientResponse response = clientGetWithFullURL(url, 200); Document<Entry> doc = response.getDocument(); Entry entry = doc.getRoot();/*w w w.j ava 2 s . co m*/ String content = entry.getContent(); assertFalse(StringUtils.isEmpty(content)); int count = StringUtils.countMatches(content, "<deletion xmlns"); log.debug("Count= " + count + "\nContent= \n" + content); assertEquals(1, count); response.release(); }
From source file:org.b3log.latke.servlet.handler.RouteHandler.java
/** * Routes the request specified by the given request URI and HTTP method. * * @param requestURI the given request URI * @param httpMethod the given HTTP method * @return MatchResult, returns {@code null} if not found *//* www . ja va 2 s. c o m*/ public static MatchResult doMatch(final String requestURI, final String httpMethod) { MatchResult ret; final int segs = StringUtils.countMatches(requestURI, "/"); ContextHandlerMeta contextHandlerMeta; String concreteKey = httpMethod + "." + requestURI; switch (segs) { case 1: contextHandlerMeta = ONE_SEG_CONCRETE_CTX_HANDLER_METAS.get(concreteKey); if (null != contextHandlerMeta) { return new MatchResult(contextHandlerMeta, requestURI, httpMethod, requestURI); } switch (httpMethod) { case "GET": return route(requestURI, httpMethod, ONE_SEG_GET_VAR_CTX_HANDLER_METAS); case "POST": return route(requestURI, httpMethod, ONE_SEG_POST_VAR_CTX_HANDLER_METAS); case "PUT": return route(requestURI, httpMethod, ONE_SEG_PUT_VAR_CTX_HANDLER_METAS); case "DELETE": return route(requestURI, httpMethod, ONE_SEG_DELETE_VAR_CTX_HANDLER_METAS); default: return route(requestURI, httpMethod, ONE_SEG_OTHER_METHOD_VAR_CTX_HANDLER_METAS); } case 2: contextHandlerMeta = TWO_SEG_CONCRETE_CTX_HANDLER_METAS.get(concreteKey); if (null != contextHandlerMeta) { return new MatchResult(contextHandlerMeta, requestURI, httpMethod, requestURI); } switch (httpMethod) { case "GET": return route(requestURI, httpMethod, TWO_SEG_GET_VAR_CTX_HANDLER_METAS); case "POST": return route(requestURI, httpMethod, TWO_SEG_POST_VAR_CTX_HANDLER_METAS); case "PUT": return route(requestURI, httpMethod, TWO_SEG_PUT_VAR_CTX_HANDLER_METAS); case "DELETE": return route(requestURI, httpMethod, TWO_SEG_DELETE_VAR_CTX_HANDLER_METAS); default: return route(requestURI, httpMethod, TWO_SEG_OTHER_METHOD_VAR_CTX_HANDLER_METAS); } case 3: contextHandlerMeta = THREE_SEG_CONCRETE_CTX_HANDLER_METAS.get(concreteKey); if (null != contextHandlerMeta) { return new MatchResult(contextHandlerMeta, requestURI, httpMethod, requestURI); } switch (httpMethod) { case "GET": return route(requestURI, httpMethod, THREE_SEG_GET_VAR_CTX_HANDLER_METAS); case "POST": return route(requestURI, httpMethod, THREE_SEG_POST_VAR_CTX_HANDLER_METAS); case "PUT": return route(requestURI, httpMethod, THREE_SEG_PUT_VAR_CTX_HANDLER_METAS); case "DELETE": return route(requestURI, httpMethod, THREE_SEG_DELETE_VAR_CTX_HANDLER_METAS); default: return route(requestURI, httpMethod, THREE_SEG_OTHER_METHOD_VAR_CTX_HANDLER_METAS); } default: contextHandlerMeta = FOUR_MORE_SEG_CONCRETE_CTX_HANDLER_METAS.get(concreteKey); if (null != contextHandlerMeta) { return new MatchResult(contextHandlerMeta, requestURI, httpMethod, requestURI); } switch (httpMethod) { case "GET": return route(requestURI, httpMethod, FOUR_MORE_SEG_GET_VAR_CTX_HANDLER_METAS); case "POST": return route(requestURI, httpMethod, FOUR_MORE_SEG_POST_VAR_CTX_HANDLER_METAS); case "PUT": return route(requestURI, httpMethod, FOUR_MORE_SEG_PUT_VAR_CTX_HANDLER_METAS); case "DELETE": return route(requestURI, httpMethod, FOUR_MORE_SEG_DELETE_VAR_CTX_HANDLER_METAS); default: return route(requestURI, httpMethod, FOUR_MORE_SEG_OTHER_METHOD_VAR_CTX_HANDLER_METAS); } } }