Add new Object with generate new id of field - java

I have list:
private List<Day> days = new ArrayList<>();
My Day object:
public class Day implements Identifiable {
private Integer id;
private byte isHoliday;
//getters and setters
}
I added a new Day object like:
Day day = new Day();
day.setHoliday(1);
days.add(day);
How to make, that at adding of a new element, field id set automatically, which equal to the previous element + 1?
Maybe I can use Java 8 Streams?

You can use AtomicInteger() - it is thread safe.
public class Day implements Identifiable {
private static AtomicInteger count = new AtomicInteger(0);
private int id;
private byte isHoliday;
public Day() {
this.id = count.incrementAndGet();
}
}

You could use a static member and a constructor:
public class Day implements Identifiable {
static private int maxId = 0;
final private Integer id;
private byte isHoliday;
public Day() {
this.id = maxId;
maxId++;
}
}
Every time you create a new instance of Day its id member is set to the value of maxId and then maxId is incremented.
Making "id" final is a good idea since it is used to identify your object.

Use a static variable say oldId to store the previous id. Change your Day class to:
public class Day implements Identifiable {
private static Integer oldId = 0;
private Integer id;
private byte isHoliday;
public Day() {
this.id = oldId + 1;
oldId++;
}
//getters and setters
}

If you can try to use simple types rather than object-oriented ones, i.e. instead of Integer to int.
public class Day {
private static int serial = 0; //static means that this is a common field (the same place in memory) for all created objects.
private final int id; //final means that another value / object can not be assigned to this reference after initializing in the constructor.
public Day() {
id = serial++;
}
//getters and setters
}

Related

how to multily two fields from two different collections

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.

How to construct a Class whose variables have almost same value for majority of the objects

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)

How to pick up data from one field based on the condition of another field

How to pick up data from one field based on the condition of another field?
As shown below, I want to populate the class 'simulation' with the data from the class 'salary', the condition is like:
simulation.id = salary.id;
Some id has simulatedSalary, some id has notsimulatedSalary, some id has both of simulated and notsimulated Salary;
if(salary.simulated == true) then simulation.simulatedSalary = salary.salary, else simulation.simulatedSalary == 0;
if(salary.simulated == false) then simulation.notsimulatedSalary = salary.salary, else simulation.notsimulatedSalary == 0;
simulation.totalSalary = sum(simulation.simulatedSalary + simulation.notsimulatedSalary).
How to implement the above condition?
to populate List<simulation> simulationList from List<salary> salaryList:
public class salary {
private Integer id;
private Boolean simulated;
private Double salary;
}
public class simulation {
private Integer id;
private Double simulatedSalary;
private Double notsimulatedSalary;
private Double totalSalary;
}
if you want to implement the above code you should use inheritance also you should use protected data fields and not private for both of your class
sample usage of inheritances: in this code, your parent class is Salary and your child class is Simulation then you must set your data fields to be protected instead of private so that the child class can use its parent class variable directly
in inheritance child class can use all the methods and fields that are public or protected by the parent class
public class Salary
{
protected Integer id;
protected Boolean simulated;
protected Double salary;
}
public class Simulation extends Salary
{
//you do not need Integer id here
private Double simulatedSalary;
private Double notsimulatedSalary;
private Double totalSalary;
}

How would I map the entire source object A to a property of destination object B using Orika mapper?

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.

Dont include Ebeans Variable in SQL Statements

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;

Categories

Resources