I have two colections in mongodb sring boot:
public class StampOperation {
#Id
private String id;
private int addedQuantity;
private int previousQuantity;
private int remainingQuantity;
#JsonFormat(pattern="dd/MM/yyyy")
private Date operationDate;
#DBRef
private TaxStamp taxStamp;
}
public class TaxStamp
{
#Id
private String id;
private String name;
private double value;
private EType stampType;
private int globalInitialQuantity;
private int minimumThreshold;
}
is there any wa to multiply addedQuantity from StampOperation by value from TaxStamp ?
If I understand your question correctly, you want to know, how to multiply two values from two different objects.
double result = stampOperation.addedQuantity * taxStamp.value;
To do this operation, you need to have the referenced instances of both classes at hand. Also, the attributes which are supposed to be used (addedQuantity and value) need to be accessible. This could be done by e.g. marking them as public.
I need to get some specific data from rest to my model.
Data I get from rest :
[{"id":595,"name":"TransXChange20210805113332","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-08-05T09:45:21.444949Z","edited":"2021-08-05T09:45:27.542152Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 08.08.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210805113332.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210805113332.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":538,"name":"TransXChange20210513082611","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-13T06:28:50.233464Z","edited":"2021-07-28T08:52:06.695966Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 15.05.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210513082611.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210513082611.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":544,"name":"TransXChange20210526143716","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-26T12:40:42.587492Z","edited":"2021-07-28T08:52:04.417450Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 01.06.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210526143716.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210526143716.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false}]
i got it in single line
my code in java :
RestTemplateBuilder builder = new RestTemplateBuilder();
String soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", String.class);
assert soc != null;
System.out.println(soc);
And the problem is I need to put them into model
My model:
public class Resource {
private long id;
private String name;
private long prosystem_file_id;
private int dataset;
private int creator;
private int editor;
private LocalDateTime created;
private LocalDateTime edited;
private boolean update_from_url;
private String description;
private String file;
private String link;
private String extension;
private String data_type;
private String file_name;
private String chart_info;
private String map_info;
private int score;
private boolean isPublic;
private String licence;
private String connection_dict;
private String selection_string;
private String db_type;
private String type;
private int dbview_cache_minutes;
private String preview_base64;
private String gpkg_display_info;
private boolean archive_to_csv;
All getters and setters are generated.
How can I put them into model for example List?
The problem is I get data like [{resource},{resource},{resource}].
As it states from the RestTemplate documentation
you can call to the getForObject method passing as an argument the desired deserialization class.
Something like:
RestTemplateBuilder builder = new RestTemplateBuilder();
Resource soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", Resource.class);
assert soc != null;
System.out.println(soc);
I have found answer to my question.
I need to assign my call to array of model.
Resource[] soc =builder.build().getForObject( URL , Resource[].class );
What is the best way to construct an Object which has only one variable being changed for multiple invocations.
public class Brand {
final private Long id;
final private String code;
final private String version;
final private String type;
final private List<String> listThatChanges;
}
Most of the times I have to change the listThatChanges and put the object inside another Collection. Im thinking having a four argument constructor like below
public Brand(id,code,version,type) {
this.id = id,
this.code = code;
this.version = version;
this.type = type;
}
and a setter for listThatChanges, but this doesn't help me in any memory Optimization as I have to create a new Object anyways.Is there a better way to efficiently create a Class so that dont have reconstruct the same(except one attribute) Object multiple times
First of all: don't optimize unless you have a need to.
Second:
If only one part changes, then that should in fact be the only thing that change, and you can reflect that by embracing composition and introducing a wrapper object.
class BrandAttributes{
final Brand brand; // this object always stays the same
final List<String> attributes; // this one changes
}
public class Brand {
final private Long id;
final private String code;
final private String version;
final private String type;
}
public class BrandWithList {
final private Brand brand;
final private List<String> listThatChanges;
}
Try to encapsulate what stays the same.
The code can be restructured, but has not much room for memory optimization
Still if you want to
Use long primitive, instead of Long Wrapper
final private long id;
Initialize the ArrayList with size 0
this.listThatChanges = new ArrayList(0)
I know there are no default arguments for methods in Java and this can be remedied by using method overloading like in this question.
However, I have a class with around twenty fields and I should be able to create that class with any combination of the fields:
import java.util.Date;
public class RequestBodyGenerator {
private Integer length;
private String author;
private String title;
private Long descriptionId;
private Long productId;
private Integer yearMin;
private Integer yearMax;
private Long publisherId;
private String publisher;
private String ean13;
private String imageFilter;
private String image;
private Date createdFrom;
private Date createdFromTime;
private Date createdTo;
private Date createdToTime;
private Date shopSellFrom;
private Date shopSellFromTime;
private Date shopSellTo;
private Date shopSellToTime;
private Integer minPrice;
private Integer maxPrice;
private String moreInfo;
private String storagePlace;
private String creator;
private String orderBy;
private Boolean __checkbox_needImage;
}
This class shall create the body of a http request, so whichever fields the constructor gets will have a value in the request body, the others should be an empty string. It is possible that there is only one field, lets say I give in a "title" field, all the other fields are empty, but it may happen that I will give value to 10+ fields.
My problem could be solved by using an empty string as a default argument, but this is not working in Java. If I wrote a constructor for each possible case I would end up writing hundreds of them, so this is obviously not the way to go.
Using the build pattern, first suggested by khelwood, was the solution to my problem.
after messing around with parsing a JSON response with GSON for a day, I finally figured out how to get my javabeans right in order to extract my data from the response. This is the layout of my nested classes and lists:
public class LocationContainer {
public class paging {
private String previous;
private String next;
}
private List<Datas> data;
public class Datas {
private String message;
private String id;
private String created_time;
public class Tags {
private List<Data> datas;
public class Data {
private String id;
private String name;
}
}
public class Application {
private String id;
private String name;
}
public class From {
private String id;
private String name;
}
public class Place {
private String id;
private String name;
public class Location {
private int longitude;
private int latitude;
}
}
}
}
Now I am trying to get a hold of the name string inside the place class and the created_time string, but since I am quite a noob, I can't seem to figure it out.
I was able to extract the created_time string by using
String time = gson.toJson(item.data.get(1).created_time);
However using
String name = gson.toJson(item.data.get(1).Place.name);
doesnt work.
The item class is an instance of LocationContainer filled with the output from GSON.
Any pointers would be greatly appreciated.
created_time is a member variable of Data, so your first line is fine.
However, Place is not a member variable, it's just a class definition. You probably need to instantiate a member variable inside your Data class, e.g.:
private Place place;