下面列出了java.util.stream.Stream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Transactional
public void importFromFile(Path savedFilePath) throws IOException {
Stream<String> lines = Files.lines(savedFilePath);
// @formatter:off
List<DicWord> dicWordsToInsert = lines.filter(line -> !line.isEmpty())
.flatMap(DicWordStreamParser::parse)
.distinct()
.collect(Collectors.toList());
// @formatter:on
dicWordRepository.deleteAll();
dicWordRepository.flush();
dicWordRepository.saveAll(dicWordsToInsert);
lines.close();
dicWordRepository.flush();
logger.debug("Imported");
}
private List<File> getSubDirectories(final File directory, DetectorSearchFilter filter) throws DetectUserFriendlyException {
Stream<Path> stream = null;
try {
stream = Files.list(directory.toPath());
return stream.map(path -> path.toFile())
.filter(file -> file.isDirectory())
.collect(Collectors.toList());
} catch (final IOException e) {
throw new DetectUserFriendlyException(String.format("Could not get the subdirectories for %s. %s", directory.getAbsolutePath(), e.getMessage()), e, ExitCodeType.FAILURE_GENERAL_ERROR);
} finally {
if (stream != null) {
stream.close();
}
}
}
private Collection<String> getKeywords() {
// SQL reserved keywords
Stream<String> tokens = Arrays.stream(SQLLexer.tokenNames);
Stream<String> rules = Arrays.stream(SQLLexer.ruleNames);
List<String> keywords = Stream.concat(tokens, rules)
.filter((str) -> str.matches("[A-Z_]+") && str.length() > 1)
.distinct()
.map(String::toLowerCase)
.collect(Collectors.toList());
// DB and table names
for (String db: client.getAllDatabaseNames()) {
keywords.add(db);
keywords.addAll(client.getTableList(db));
}
tokens.close();
rules.close();
return keywords;
}
@Test
public void testMapJsontoBdataKey() throws Exception
{
String fileName = "sqsMessage.txt";
String expectedNamespace = "DMFormatNamespace1";
Path path = Paths.get(getClass().getClassLoader().getResource(fileName).toURI());
Stream<String> lines = Files.lines(path);
String messageBody = lines.collect(Collectors.joining("\n")).trim();
lines.close();
BusinessObjectDataKey bdataKey = herdApiClientOperations.mapJsontoBdataKey(messageBody).getBusinessObjectDataKey();
Assert.assertEquals("Did not get the correct namespace", expectedNamespace, bdataKey.getNamespace());
}
/**
* All edges have id=0 - this causes an exception. Replace all zero ids and save the file
*
* @param file2
*/
private void removeZeroIDFromEdge(File file) {
try {
logger.info("replacing zero ids in graphml");
Path path = Paths.get(file.getAbsolutePath());
Stream<String> lines = Files.lines(path);
List<String> replaced =
lines.map(line -> line.replaceAll("edge id=\"0\"", "edge")).collect(Collectors.toList());
Files.write(path, replaced);
lines.close();
logger.info("zero ids in graphml replaces");
} catch (IOException e) {
logger.log(Level.SEVERE, "graphml NOT LOADED: " + file.getAbsolutePath(), e);
setErrorMessage("Cannot load graphml file: " + file.getAbsolutePath());
setStatus(TaskStatus.ERROR);
cancel();
}
}
/**
* Loads a list of tests based on the json-encoded test vector files.
*/
@Parameters(name = "Test Vector {index}: {0}")
public static Collection<StreamTestFixture> testVectorData() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new GuavaModule());
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Properties properties = readProperties();
String fileName = (String) properties.get("stream.packetFixtures.fileName");
Path path = Paths.get(StreamPacketFixturesTest.class.getClassLoader().getResource(fileName).toURI());
Stream<String> lines = Files.lines(path);
String data = lines.collect(Collectors.joining("\n"));
lines.close();
List<StreamTestFixture> vectors = objectMapper.readValue(data, new TypeReference<List<StreamTestFixture>>() {});
return vectors;
}
@VisibleForTesting
String createFingerprint(final int line, final Stream<String> lines, final Charset charset) {
String context = extractContext(line, lines.iterator());
lines.close();
digest.update(context.getBytes(charset));
return asHex(digest.digest()).toUpperCase(Locale.ENGLISH);
}
private static List<String> getJavaFilePaths(File folder) throws IOException {
Stream<Path> walk = Files.walk(Paths.get(folder.toURI()));
List<String> paths = walk.map(x -> x.toString())
.filter(f -> f.endsWith(".java"))
.map(x -> x.substring(folder.getPath().length()+1).replaceAll(systemFileSeparator, "/"))
.collect(Collectors.toList());
walk.close();
return paths;
}
@Test
public void relationQuadRetractionRemovesTheQuadFromTheStore() throws Exception {
tripleStore.putQuad(EX + "subject1", "http://pred", Direction.OUT, EX + "subject1", null, null);
tripleStore.putQuad(EX + "subject1", "http://pred", Direction.OUT, EX + "subject2", null, null);
tripleStore.deleteQuad(EX + "subject1", "http://pred", Direction.OUT, EX + "subject2", null, null);
Stream<CursorQuad> quads = tripleStore.getQuads(EX + "subject1", "http://pred", Direction.OUT, "");
assertThat(quads.collect(toList()), not(hasItem(
create(EX + "subject1", "http://pred", Direction.OUT, EX + "subject2", null, null, "http://some graph")
)));
quads.close();
}
@Test
public void getItemFromValue() throws Exception {
final Stream<String> stream = database.databaseGetter()
.key("ab")
.skipToValue("bc")
.forwards()
.getValues(database.valueRetriever());
final long count = stream
.count();
stream.close();
assertThat(count, is(2L));
}
@Test
public void testDSLMapToStreamFromString() throws IOException {
final Stream<Tuple2<String, String>> stream = CsvParser.mapTo(String.class, String.class)
.headers("0", "1").stream("value1,value2\nvalue3");
List<Tuple2<String, String>> list = stream.collect(Collectors.toList());
stream.close();
assertArrayEquals(new Object[] { new Tuple2<String, String>("value1", "value2"), new Tuple2<String, String>("value3", null)}, list.toArray());
}
private String toString(Stream<Book> books) {
try {
return books
.map(Book::toString)
.collect(joining("\n - ", "\n - ", ""));
} finally {
// We have to close the stream when finished to free the resources used by the query.
books.close();
}
}
private IFunctionHandler matchFunctionHandler(NettyHttpRequest request) throws IllegalPathNotFoundException, IllegalMethodNotAllowedException {
AtomicBoolean matched = new AtomicBoolean(false);
Stream<Path> stream = functionHandlerMap.keySet().stream()
.filter(((Predicate<Path>) path -> {
/**
*过滤 Path URI 不匹配的
*/
if (request.matched(path.getUri(), path.isEqual())) {
matched.set(true);
return matched.get();
}
return false;
}).and(path -> {
/**
* 过滤 Method 匹配的
*/
return request.isAllowed(path.getMethod());
}));
Optional<Path> optional = stream.findFirst();
stream.close();
if (!optional.isPresent() && !matched.get()){
throw new IllegalPathNotFoundException();
}
if (!optional.isPresent() && matched.get()){
throw new IllegalMethodNotAllowedException();
}
return functionHandlerMap.get(optional.get());
}
private AsyncFuture<Void> askForOk(
final ShellIO io, final Stream<BackendKey> keys
) {
io
.out()
.println("Examples of keys that would have been deleted (use --ok to " + "perform):");
keys.limit(100).forEach(k -> {
io.out().println(k.toString());
});
keys.close();
return async.resolved();
}
private static int determineNumberOfObjectives(String inputFileName) {
Stream<String> lines ;
try {
lines = Files.lines(Paths.get(inputFileName), Charset.defaultCharset());
} catch (IOException e) {
throw new JMetalException(e) ;
}
int numberOfObjectives = lines.findFirst().get().split(" ").length ;
lines.close();
return numberOfObjectives;
}
@Test
public void getAllItems() throws Exception {
final Stream<String> stream = database.databaseGetter()
.getAll()
.getValues(database.valueRetriever());
final long count = stream
.count();
stream.close();
assertThat(count, is(6L));
}
public void viewFileContent(String empFile) throws IOException{
Consumer<String> readStr = System.out::println;
Stream<String> content = null;
Path path = Paths.get(empFile);
content = Files.lines(path, Charset.defaultCharset());
content.map(String::toUpperCase)
.forEach(readStr);
content.close();
}
@Test
public void langStringQuadRetractionQuadRemovesTheQuadFromTheStore() throws Exception {
tripleStore.putQuad(EX + "subject1", "http://pred", Direction.OUT, "Walter", LANGSTRING, "EN-en");
tripleStore.putQuad(EX + "subject1", "http://pred", Direction.OUT, "Gauthier", LANGSTRING, "FR-fr");
tripleStore.deleteQuad(EX + "subject1", "http://pred", Direction.OUT, "Walter", LANGSTRING, "EN-en");
Stream<CursorQuad> quads = tripleStore.getQuads(EX + "subject1", "http://pred", Direction.OUT, "");
assertThat(quads.collect(toList()), not(hasItem(
create(EX + "subject1", "http://pred", Direction.OUT, "Walter", LANGSTRING, "EN-en", "http://some graph")
)));
quads.close();
}
void run(String[] args) {
try {
itemCount = 0;
// alternative to supplying args- hard code the path :-)
if (args == null || args.length == 0) {
d1 = Paths.get("some-explicit-coded-path/uv2-out-some-suffix");
d2 = Paths.get("some-explicit-coded-path/uv2-out-some-other-suffix");
d1 = Paths.get("c:/a/t/ipd2018/uv2-out-b4-2");
// d2 = Paths.get("c:/a/t/ipd2018/uv2-out-b4");
// d1 = Paths.get("c:/a/t/ipd2018/uv2-out-measAnnot-fsiter-2c-getSurroundSent");
d2 = Paths.get("c:/a/t/ipd2018/uv2-out-measAnnot-fsiter-2d-getSurroundSent-partial");
d1 = Paths.get("c:/a/t/ipd2018/uv2-out-b4");
d1 = Paths.get("c:/a/t/ipd2018/uv2-out-jp-merge-outer");
d2 = Paths.get("c:/a/t/ipd2018/uv2-out-jp-merge");
// skip = 725; // optional skip amount
} else {
d1 = Paths.get(args[0]);
d2 = Paths.get(args[1]);
skip = Integer.parseInt(args[2]);
}
d1String = d1.toString();
d2String = d2.toString();
boolean d1v2 = d1String.contains("uv2-out");
boolean d2v2 = d2String.contains("uv2-out");
boolean d1v3 = d1String.contains("uv3-out");
boolean d2v3 = d2String.contains("uv3-out");
isV2V2 = d1v2 && d2v2;
isV3V3 = d1v3 && d2v3;
isV2V3 = (d1v2 && d2v3) || (d1v3 && d2v2);
System.out.println("Comparing " + d1String + " to " + d2String);
if (isV2V2) System.out.println("\napplying fixups for v2 versus v2 comparison");
if (isV2V3) System.out.println("\napplying fixups for v2 versus v3 comparison");
if (isV3V3) System.out.println("\napplying fixups for v3 versus v3 comparison");
// read the type system descriptor
File typeSystemFile = Paths.get(d2String, "CAS_typeSystem_desc.xml").toFile();
TypeSystemDescription typeSystemDescription = UIMAFramework.getXMLParser().parseTypeSystemDescription(
new XMLInputSource(typeSystemFile));
c1 = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, null, null);
c2 = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, null, null);
Stream<Path> pathStream = Files.walk(d1, FileVisitOption.FOLLOW_LINKS);
pathStream.forEach(p1 -> maybeCompare(p1));
pathStream.close();
} catch (ResourceInitializationException | InvalidXMLException | IOException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the <b>full</b> data set content for given id.
*
* @param metadata If <code>true</code>, includes data set metadata information.
* @param dataSetId A data set id.
* @return The full data set.
*/
@RequestMapping(value = "/datasets/{id}/content", method = RequestMethod.GET)
@ApiOperation(value = "Get a data set by id",
notes = "Get a data set content based on provided id. Id should be a UUID returned by the list operation. Not valid or non existing data set id returns empty content.")
@Timed
@ResponseBody
public Callable<DataSet> get(
@RequestParam(defaultValue = "true") @ApiParam(name = "metadata",
value = "Include metadata information in the response") boolean metadata, //
@RequestParam(defaultValue = "false") @ApiParam(name = "includeInternalContent",
value = "Include internal content in the response") boolean includeInternalContent, //
@RequestParam(defaultValue = "-1") @ApiParam(name = STORAGE_LIMIT, value = STORAGE_LIMIT) long limit, //
@ApiParam(value = "Filter for retrieved content.") @RequestParam(value = "filter",
defaultValue = "") String filter,
@PathVariable(value = "id") @ApiParam(name = "id",
value = "Id of the requested data set") String dataSetId) {
return () -> {
final Marker marker = Markers.dataset(dataSetId);
LOG.debug(marker, "Get data set #{}", dataSetId);
Stream<DataSetRow> stream = null;
try {
DataSetMetadata dataSetMetadata = dataSetMetadataRepository.get(dataSetId);
assertDataSetMetadata(dataSetMetadata, dataSetId);
// Build the result
DataSet dataSet = new DataSet();
if (metadata) {
dataSet.setMetadata(conversionService.convert(dataSetMetadata, UserDataSetMetadata.class));
}
stream = contentStore.stream(dataSetMetadata, limit); // Disable line limit
// on-demand analyzer for dataset (See TDP-4404, migration problems)
if (dataSetMetadata.getRowMetadata().getColumns().stream().anyMatch(
c -> c.getStatistics().getWordPatternFrequencyTable().isEmpty())) {
stream = insertWordPatternAnalysis(dataSetMetadata, stream);
}
if (!includeInternalContent) {
LOG.debug("Skip internal content when serving data set #{} content.", dataSetId);
stream = stream.map(r -> {
final Map<String, Object> values = r.values();
final Map<String, Object> filteredValues = new HashMap<>(values);
// Remove technical properties from returned values.
values.forEach((k, v) -> {
if (k != null && k.startsWith(FlagNames.INTERNAL_PROPERTY_PREFIX)) {
filteredValues.remove(k);
}
});
filteredValues.put(FlagNames.TDP_ID, r.getTdpId()); // Include TDP_ID anyway
return new DataSetRow(r.getRowMetadata(), filteredValues);
});
}
// Filter content
stream = stream.filter(filterService.build(filter, dataSetMetadata.getRowMetadata()));
dataSet.setRecords(stream);
return dataSet;
} catch (Exception e) {
if (stream != null) {
stream.close();
}
throw e;
} finally {
LOG.debug(marker, "Get done.");
}
};
}