Map Java Stream to Map of Objects with Object as Key - java-8

I have some classes and I am doing some work in a List<WorkDay> which contains a List<LedgerItem>, I have everything working but one part. Well it works, but not exactly how I would like it to.
public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) {
return workDays.stream()
.sorted((d1,d2) -> d1.getCreated().compareTo(d2.getCreated()))
.map(day -> createGroupByWorkDay(day))/*need it to collect here*/;
}
If you can see the return type is Stream<Map<WorkDay, Set<LedgerItem>>> but I want to map this out of the Stream as Map<WorkDay, Set<LedgerItem>> with a collector but just cannot seem to get Collectors.toMap() syntax to do anything but break.
Like I said, everything works perfectly, so I dont need anything outside of the mapping to work.
Just FYI: createGroupByWorkDay returns Map<WorkDay, Set<LedgerItem>> already but only accepts a single WorkDay as this is a requirement so I cannot change the way this is executed...
thanks in advance
EDIT:
So the method that I have createGroupByWorkDay that is not listed here works perfectly as expected, and will never be changed. It returns the correct type of Map<WorkDay, Set<LedgerItem>> but only has a signature for one WorkDay like this createGroupByWorkDay(WorkDay day) to the method in question in the original comment uses that to build individual Maps which are grouped by WorkDay and returns, but there could be N number of those, so the method public Stream<Map<WorkDay, Set<LedgerItem>>> adjustWorkDays(List<WorkDay> workDays) should return all of those Maps collected into one map in the collector. If that makes any sense?

Your question is not clear to me. But I guess you may be asking for something like this?
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.collect(Collectors.toMap(Function.identity(), WorkDay::getLedgerItems));
If not please explain your problem statement clearly. This is just a guess.
Here's an update,
Map<WorkDay, List<LedgerItem>> result = workDays.stream()
.map(d -> createGroupByWorkDay(d))
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Related

Issues adding NEAR tokens in assemblyscript smart contract using u128.add() function

I have been facing issues with the use of u128.add(a, b) function. The two u128 values do not get added and I am afraid I am doing something wrong. I have checked LEARN-NEAR github page for sample projects and even changed my code to follow the patterns used, however the values don't get added.
signWithFunds(amount: u128): bool {
assert(context.sender, "Petition must be signed by an account");
assert(this.isFunded, `not a funded petition`);
assert(u128.ge(amount, this.minFundAmount),
`amount provided is less than minimum funding amount: at least ${asNEAR(this.minFundAmount)} required to sign this petition`);
const currentTotal = this.funding;
this.funding = u128.add(amount, currentTotal);
this.signature.push(context.sender);
return true;
}
model.ts
main.ts
aspect test file
test result showing unexpected behaviour
From the images, it looks like the test aren't receiving the expected values. The test receives 0, but expected some other values. I don't think there's anything wrong with the u128add function in itself.
From the test, you are calling a function that relies on Context's deposit, I think you need to add that to your test *(it("should sign a funded ...."), as well:
VMContext.setAttached_deposit(someDeposit)
Second, signWithFunds is relies on this.founding as well, which I believe is the funding in the petition itself. Maybe petitions[0] in your test isn't the newly created petition? We need to look at the beforeEach function to make sure, because otherwise, you are adding a new petition to the array, but you're referencing an older one.
I discovered that the 7th line of signWithFunds should have been this.funding.set(u128.add(amount, currentTotal));

how to convert a Flux<Object> list into a List<Object>

I have a Flux and I want to convert it to List. How can I do that?
Flux<Object> getInstances(String serviceId); // Current one
List<Object> getInstances(String serviceId); // Demanded one
Java 8 or reactive components have a prepared method to map or convert it to List ??
I should use .map()
final List<ServiceInstance> sis = convertedStringList.parallelStream()
.map( this.reactiveDiscoveryClient::getInstances )
// It should be converted to List<Object>
1. Make sure you want this
A fair warning before diving into anything else: Converting a Flux to a List/Stream makes the whole thing not reactive in the strict sense of the concept because you are leaving the push domain and trading it with a pull domain. You may or may not want this (usually you don't) depending on the use-case. Just wanted to leave the note.
2. Converting a Flux to a List
According to the Flux documentation, the collectList method will return a Mono<List<T>>. It will return immediately, but it's not the resulting list itself, but a lazy structure, the Mono, that promises the result will eventually be there when the sequence is completed.
According to the Mono documentation, the block method will return the contents of the Mono when it completes. Keep in mind that block may return null.
Combining both, you could use someFlux.collectList().block(). Provided that someFlux is a Flux<Object>, the result would be a List<Object>.
The block method won't return anything if the Flux is infinite. As an example, the following will return a list with two words:
Flux.fromArray(new String[]{"foo", "bar"}).collectList().block()
But the following will never return:
Flux.interval(Duration.ofMillis(1000)).collectList().block()
To prevent blocking indefinitely or for too long, you may pass a Duration argument to block, but that will timeout with an exception when the subscription does not complete on time.
3. Converting a Flux to a Stream
According to the Flux documentation, the toStream method converts a Flux<T> into a Stream<T>. This is more friendly to operators such as flatMap. Mind this simple example, for the sake of demonstration:
Stream.of("f")
.flatMap(letter ->
Flux.fromArray(new String[]{"foo", "bar"})
.filter(word -> word.startsWith(letter)).toStream())
.collect(Collectors.toList())
One could simply use .collectList().block().stream(), but not only it's less readable, but it could also result in NPE if block returned null. This approach does not finish for an infinite Flux as well, but because this is a stream of unknown size, you can still use some operations on it before it's complete, without blocking.

parse.com - cloud code - what do i do with a pointer?

Someone doesn't understand what i want. I want to know what i can do with a pointer.
I have js cloud code.
I have a pointer.
What can i do with it?
Example:
var query = new Parse.Query("Messages"); //POINTER QUERY
console.log(userMessages[0].get("messageId"));
console.log("end2");
query.equalTo("objectId",userMessages[position].get("messageId"));
In the example, userMessages is the result of a prior cloud query.
This line
console.log(userMessages[0].get("messageId"));
helpfully outputs
{"__type":"Pointer","className":"Messages","objectId":"5J4eOletgz"}
This is less useful than you might imagine. I cannot seem to call the objectId from it, and the query
query.equalTo("objectId",userMessages[position].get("messageId"));
query.find ({ ... });
returns nothing. Note that the query should find the pointer-object the pointer-points-to, but instead it helpfully throws the error
Error: 102 bad special key: __type
Which is just about useless.
What can i do with a pointer?
Why don't the people at parse.com bother to write this stuff up anywhere?
That second question is more like a buddhist koan for them to meditate over, no need to respond!
you can use:
userMessagesQuery.include("messageId")
before you execute your query that returns userMessages and you will get the entire object in "messageId" instead of just a pointer.
Also you use
userMessages[0].get("messageId").fetch({success:function(){}})
to get the full object if you don't want to use "include"
Suggestion: I'd rename "messageId" to "message" to make it clear that it's an object pointer and not an ID field.
A pointer is a Parse object with just the minimal data used for linking. If you want to get the rest of the data for a pointer (fully populate it), use the fetch() method.
If you just want the objectId from a pointer, you retrieve it just like you would any other Parse object, by using the myPointer.objectId property.
In your case the following would work, but isn't the most optimal solution:
// I would suggest renaming messageId if you're actually storing pointers
var messagePointer = userMessages[position].get("messageId");
query.equalTo("objectId", messagePointer.objectId);
Instead, as stated by #RipRyness, you could just change your previous query to include() the full object, avoiding many extra queries.
userMessagesQuery.include("messageId");
// ... later in success handler ...
// now a fully populated Message object
var message = userMessages[position].get("messageId");
console.log(message);
Okay so a pointer is does not only point to a certain object, but it can BE that object.
In the query that returns the userMessages result, you can use the line .include("messageId") - this makes your query not only return a pointer to that object, it actually includes that object in place of the pointer.
After using the include statement, userMessages[0].get("messageId") will return the Message object linked that that userMessage object. You no longer need to query the Message collection to get the object.

How to read pair in freemarker

I'm having little trouble with reading pair.
So I'm creating my pair
private Pair<Integer, Integer> count(somethink) {
int c1 = 2;
int c2 = 4;
return new Pair<Integer, Integer>(c1, c2);
}
And 'sending' it to ftl via java
mv.addObject("counted", count(somethink));
I won't write everything how it sends because I don't think it really matters with my issue. So i'm recieving it in "ftl". Then I was trying to 'read' it.
<#list counted?keys as key>
<#spring.message "someMsg"/>(${key}/${counted[key]})
</#list>
After then I'm getting error
Expecting a string, date or number here, Expression x is instead a freemarker.ext.beans.SimpleMethodModel
As I suppose you don't iterate pairs (or I'm wrong?) I know its pair that contains only one key and one value but still I have to do send it that way and I thought its going be to similar to iterating through map, in Java I would use pair.first() and pair.second() but it doesn't work in ftl (yes I know it shouldn't work). I also tried to cast it to String by using ?string but it didnt work too
have you tried?
${counted.first()?xml}/${counted.second()?xml}
Assuming pair.first() and second() work in your Java code.
I'm not sure how the API of Pair looks, but with ?keys you probably unwillingly list its methods. Hence the error message; you try to print the method itself, not its return value. (As of the broken error message, because there's no x there, a FreeMarker update would help.) So as Caleryn says, just call the two methods. If you need to list this thing, you probably need to put it into a List or array, unless Pair implements List (or even just Collection) or Iterable. Or if this is a bigger issue in your project, FreeMarker has this pluggable configuration component, the ObjectWrapper, and with that you can tell FreeMarker to treat Pair-s as lists, and how. That needs some deeper understanding of FreeMarker though.

Refactor my ruby snippet so it doesn't look like C anymore: method(method(param))

I have a class which uses a connection object to send the request data created by a request_builder object.
The code looks like this:
connection.send_request(request_builder.build_request(customer))
This in turn is called by
build_report(customer, connection.send_request(request_builder.build_request(customer)))
Ugly! Any ideas on how to make it more expressive? Usually in ruby and OOP we chain objects like this: "string".make_it_bigger.flash_it.send
It's code, that how it looks. But you can make yourself a favour by not trying to cram everything together on one line:
request = request_builder.build_request(customer)
response = connection.send_request(request)
report = build_report(customer, response)
if you told us more about your code base we might be able to suggest something else, but you don't give us very much to go on. What does the request_builder object do? Does connection.send_request(...) return a response? Why does a report need a customer and a response (assuming that's what is returned by connection.send_request(...)), and so on.
build_report(customer, request_builder.build_request(customer).send_over(connection))

Resources