use of java.util.Objects in project jgnash by ccavanaugh.
the class Engine method emptyTrash.
/**
* Empty the trash if any objects are older than the defined time.
*/
private void emptyTrash() {
if (backGroundCounter.incrementAndGet() == 1) {
messageBus.fireEvent(new Message(MessageChannel.SYSTEM, ChannelEvent.BACKGROUND_PROCESS_STARTED, Engine.this));
}
dataLock.writeLock().lock();
try {
logger.info("Checking for trash");
final List<TrashObject> trash = getTrashDAO().getTrashObjects();
/* always sort by the timestamp of the trash object to prevent
* foreign key removal exceptions when multiple related accounts
* or objects are removed */
Collections.sort(trash);
if (trash.isEmpty()) {
logger.info("No trash was found");
}
trash.stream().filter(o -> ChronoUnit.MILLIS.between(o.getDate(), LocalDateTime.now()) >= MAXIMUM_TRASH_AGE).forEach(o -> getTrashDAO().remove(o));
} finally {
dataLock.writeLock().unlock();
if (backGroundCounter.decrementAndGet() == 0) {
messageBus.fireEvent(new Message(MessageChannel.SYSTEM, ChannelEvent.BACKGROUND_PROCESS_STOPPED, Engine.this));
}
}
}
use of java.util.Objects in project jgnash by ccavanaugh.
the class TableViewManager method getCalculatedColumnWidth.
/**
* Determines the preferred width of the column including contents.
*
* @param column {@code TableColumn} to measure content
* @return preferred width
*/
private double getCalculatedColumnWidth(final TableColumnBase<S, ?> column) {
double maxWidth = 0;
/* Collect all the unique cell items and remove null*/
final Set<Object> cellItems = new HashSet<>();
for (int i = 0; i < tableView.getItems().size(); i++) {
cellItems.add(column.getCellData(i));
}
cellItems.remove(null);
if (cellItems.size() > 0) {
// don't try if there is no data or the stream function will throw an error
final OptionalDouble max = cellItems.parallelStream().filter(Objects::nonNull).mapToDouble(o -> {
final Format format = columnFormatFactory.get().call(column);
return JavaFXUtils.getDisplayedTextWidth(format != null ? format.format(o) : o.toString(), column.getStyle());
}).max();
maxWidth = max.isPresent() ? max.getAsDouble() : 0;
}
//noinspection SuspiciousMethodCalls
maxWidth = Math.max(maxWidth, Math.max(column.getMinWidth(), minimumColumnWidthFactory.get().call(tableView.getColumns().indexOf(column))));
// header text width
maxWidth = Math.max(maxWidth, JavaFXUtils.getDisplayedTextWidth(column.getText(), column.getStyle()) * BOLD_MULTIPLIER);
return Math.ceil(maxWidth + COLUMN_PADDING);
}
use of java.util.Objects in project ddf by codice.
the class ValidateCommand method getMetacardsFromCatalog.
private List<Metacard> getMetacardsFromCatalog() throws Exception {
List<Metacard> results = new ArrayList<>();
QueryImpl query = new QueryImpl(getFilter());
ThreadContext.bind(Security.getInstance().getSystemSubject());
SourceResponse response = getCatalog().query(new QueryRequestImpl(query));
List<Result> resultList = response.getResults();
if (resultList != null) {
results.addAll(resultList.stream().map(Result::getMetacard).filter(Objects::nonNull).collect(Collectors.toList()));
}
return results;
}
use of java.util.Objects in project ddf by codice.
the class CqlResult method countMatches.
private void countMatches(Set<SearchTerm> searchTerms, Metacard mc) {
List<String> textAttributes = mc.getMetacardType().getAttributeDescriptors().stream().filter(CqlResult::isTextAttribute).filter(Objects::nonNull).map(descriptor -> mc.getAttribute(descriptor.getName())).filter(Objects::nonNull).map(attribute -> Optional.ofNullable(attribute.getValue())).filter(Optional::isPresent).map(Optional::get).map(Object::toString).collect(Collectors.toList());
List<SearchTerm> terms = searchTerms.stream().filter(term -> !"*".equals(term.getTerm())).collect(Collectors.toList());
int totalTokens = 0;
for (String value : textAttributes) {
BufferedReader reader = new BufferedReader(new StringReader(value.toLowerCase()));
String line;
try {
while ((line = reader.readLine()) != null) {
String[] tokens = line.split("[\\s\\p{Punct}]+");
for (String token : tokens) {
totalTokens++;
for (SearchTerm term : terms) {
if (term.match(token)) {
matches.put(term.getTerm(), matches.getOrDefault(term.getTerm(), 0) + 1);
}
}
}
}
} catch (IOException e) {
LOGGER.debug("Unable to read line", e);
}
matches.put("*", totalTokens);
}
}
use of java.util.Objects in project ddf by codice.
the class Associated method putAssociations.
public void putAssociations(String id, Collection<Edge> edges) throws UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
Collection<Edge> oldEdges = getAssociations(id);
List<String> ids = Stream.concat(oldEdges.stream(), edges.stream()).flatMap(e -> Stream.of(e.child, e.parent)).filter(Objects::nonNull).map(m -> m.get(Metacard.ID)).filter(Objects::nonNull).map(Object::toString).distinct().collect(Collectors.toList());
Map<String, Metacard> metacards = util.getMetacards(ids, getNonrestrictedTagsFilter()).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getMetacard()));
Map<String, Metacard> changedMetacards = new HashMap<>();
Set<Edge> oldEdgeSet = new HashSet<>(oldEdges);
Set<Edge> newEdgeSet = new HashSet<>(edges);
Set<Edge> oldDiff = Sets.difference(oldEdgeSet, newEdgeSet);
Set<Edge> newDiff = Sets.difference(newEdgeSet, oldEdgeSet);
for (Edge edge : oldDiff) {
removeEdge(edge, metacards, changedMetacards);
}
for (Edge edge : newDiff) {
addEdge(edge, metacards, changedMetacards);
}
if (changedMetacards.isEmpty()) {
return;
}
catalogFramework.update(new UpdateRequestImpl(changedMetacards.keySet().toArray(new String[0]), new ArrayList<>(changedMetacards.values())));
}
Aggregations