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.
Related
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.
I would like to map the following two classes using Orika:
public class PagedList<T> extends ArrayList<T> {
private static final long serialVersionUID = 1L;
#JsonProperty("total")
private Integer total;
#JsonProperty("offset")
private Integer offset;
#JsonProperty("count")
private Integer count = PagingService.PAGE_SIZE_DEFAULT_VALUE;
...
and
public class ListUsersResult {
private List<UserDto> users;
private Integer total;
private Integer offset;
private Integer pageSize;
...
}
so that the entire PagedList ends up in users property on ListUsersResult along with total, object and pageSize. I only see ways of mapping fields/properties from source to destination. Ideally I would like not to use customize method.
I have an Ebeans Entity class which looks like this:
#Entity
public class User {
#Id
private Long userid;
#Constraints.Required
private String username;
private boolean active;
private String img;
private String status;
private int value;
private int gender; // 0 = female, 1 = male
private int orientation; // 0 = straight, 1 = gay, 2 = bi
private int listIndex; // used to store listindex for page references
private int precessor; // used to link the pages
private int sucessor;
private static final int USER_AMOUNT = 50;
/* FINDER */
public static Model.Finder<Long,User> find = new Model.Finder<Long, User>(
Long.class, User.class
);
the listIndex precessor and sucessor variables are needed in the object, but do not exist in the database. The Finder believes they are, which makes my SQL Statements fail.
So my question is can I somehow tell the Finder NOT to include this three variables in the SQL statments?
Use #Transient annotation on fields you don't want to persist, like
#Transient
private int listIndex;
I'm very sorry for i explained like fool...
I mean I can't compile because of error.
public class PictureBean implements Serializable
{
public enum CCLtype implements Serializable
{
BY, ND, CO, NN, SA
}
private String picture_rm;
private String picture_url;
private String picture_16_9_url;
private String author;
private String title;
private String description;
private String p_memo;
private String p_original_rate;
private String move_url;
private int like_count;
private int picture_exp_count;
private List<JsonObject> tagList;
private List<JsonObject> categoryList;
private CCLtype ccl;
private String picture_source;
private boolean isUserLiked;
private boolean isUserAdded;
I use this PictureBean.class for contain image datas in my server.
but when I send list of PictureBean data to activity from activity, like below, Eclipse occurred error line.
How could I send list of PictureBean to activity from activity?
'mItems''s dataType is List of PictureBean.
intent.putExtra(CategoryDetailPagerActivity.EXTRA_DATA_CATEGORY_DETAIL_LIST, mItems);
Update:
Don't put the list it self as an extra. send a class object that contain the list as the only member as follow:
public class SerialObject implements Serializable
{
private static final long serialVersionUID = -3975620301776205681L;
public List<PictureBean> myItems;
}
SerialObject sb = new SerialObject();
sb.myItems = mItems;
intent.putExtra(CategoryDetailPagerActivity.EXTRA_DATA_CATEGORY_DETAIL_LIST, sb);
Original Answer:
This is how your class should be:
public class PictureBean implements Serializable
{
private static final long serialVersionUID = 6969933524911660214L;
public enum CCLtype
{
BY, ND, CO, NN, SA
}
private String picture_rm;
private String picture_url;
private String picture_16_9_url;
private String author;
private String title;
private String description;
private String p_memo;
private String p_original_rate;
private String move_url;
private int like_count;
private int picture_exp_count;
private List<JsonObject> tagList;
private List<JsonObject> categoryList;
private CCLtype ccl;
private String picture_source;
private boolean isUserLiked;
private boolean isUserAdded;
}
Same thing for the JsonObject class.
I believe that you don't have to serialise CCLtype
Can you share the error with us? That would be most helpful. Without that information it'd be hard to tell, but my guess is maybe that some of the class types you are trying to make serializable are not types that you can serialize in Android (at least not using the pre-made "Serializable" implementation.
Try commenting out all the types and one at a time uncommenting to find out which type is breaking your serialization. You might be able to write a special serialization method for those types (or switch it to a different type).
Just a guess because you've not yet posted the error, but as the Android docs say "if the local serialVersionUID differs from the serialVersionUID in the serialized data, deserialization will fail with an InvalidClassException." You can work around this, as I had to in my app, by declaring an explicit ID. Put this in your class, to do so, with any Long value you like:
private static final long serialVersionUID = 0L;