My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.ctp.arquilliandemo.ex1.domain;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Basic;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapKeyJoinColumn;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
*
* @author Bartosz Majsak
*
*/
@Entity
public class User implements Serializable {

private static final long serialVersionUID = -8643528344705097702L;

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Basic
@NotNull
@Size(min = 3, max = 20)
private String username;

@Basic
@NotNull
@Size(min = 8, max = 20)
private String password;

@Basic
@NotNull
@Size(max = 128)
private String firstname;

@Basic
@NotNull
@Size(max = 128)
private String lastname;

@ElementCollection
@CollectionTable(name="USER_SHARES")
@Column(name="AMOUNT")
@MapKeyJoinColumn(name="SHARE_ID")
private Map<Share, Integer> portfolio = new HashMap<Share, Integer>();


public void addShares(Share share, Integer amount) {
Integer current = Integer.valueOf(0);
if (portfolio.containsKey(share)) {
current = portfolio.get(share);
}

portfolio.put(share, current + amount);

}

/**
* Removes given amount of shares. If amount is greater then currently stored
* value {@link IllegalArgumentException} will be thrown
*
* @param share
* @param amount of shares to be removed from portfolio
*
* @throws IllegalArgumentException if amount is greater then currently stored
* value
*/
public void removeShares(Share share, Integer amount) {
Integer current = Integer.valueOf(0);
if (portfolio.containsKey(share)) {
current = portfolio.get(share);
}

int newAmount = current - amount;
if (newAmount < 0) {
throw new IllegalArgumentException("Current amount of shares is smaller than amount to be removed.");
}
portfolio.put(share, newAmount);

}

public Integer getSharesAmount(Share share) {
if (portfolio.containsKey(share)) {
return portfolio.get(share);
}

return Integer.valueOf(0);

}

public Long getId() {
return id;
}

void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public Map<Share, Integer> getPortfolio() {
return Collections.unmodifiableMap(portfolio);
}

void setPortfolio(Map<Share, Integer> portfolio) {
this.portfolio = portfolio;
}

}

Change log

r95 by bartosz.majsak on Aug 11, 2010   Diff
User entity serializable
Go to: 
Project members, sign in to write a code review

Older revisions

r89 by bartosz.majsak on Jul 12, 2010   Diff
Removing shares throws
IllegalArgumentException when amount
to be removed is greater that amount
of shares currently stored in the
portfolio
r83 by bartosz.majsak on Jul 8, 2010   Diff
Example project for arquillian blog
post - initial version.
All revisions of this file

File info

Size: 3601 bytes, 151 lines
Powered by Google Project Hosting