In Tests none of the threads execute the method - java

I got a project, but I see difficulties. https://github.com/siqqQ/ILTasks/tree/master/busThread This is the project(4 files). I am worried about this test
`
void areAllPeopleWithDistinctTicket() throws InterruptedException {
int numberOfPeople = 50;
TicketManager t = TicketManager.getTicketManager(numberOfPeople);
Set<Integer> persons = new HashSet<>();
t.start();
for (Person person : t.getPersons()) {
if(!persons.add(person.getTicketNumber()) && person.getTicketNumber()!=null){
fail("There are people with same tickets");
}
}
}
When I test it, it shows me in the console that all tickets are SoldOut and none of the people are on the bus.
`
Name : Person 1 | Ticket: SoldOut
Name : Person 2 | Ticket: SoldOut
Name : Person 3 | Ticket: SoldOut
Name : Person 4 | Ticket: SoldOut
Name : Person 5 | Ticket: SoldOut
Name : Person 6 | Ticket: SoldOut
Name : Person 7 | Ticket: SoldOut
Name : Person 8 | Ticket: SoldOut
Name : Person 9 | Ticket: SoldOut
Name : Person 10 | Ticket: SoldOut
Name : Person 11 | Ticket: SoldOut
Name : Person 12 | Ticket: SoldOut...
`
I will be happy if someone can explain me why is this happening and provide some solution to this.

In TicketManager, you're calling printBusArrangment() without waiting for the threads to finish executing. Try this:
sendAllPeopleToBuyTicket(personsThreads, persons);
for (Thread thread : personThreads) {
thread.join();
}
printBusArrangment(persons);

Related

Groovy createCriteria issue with joined table

I have a domain class Coach which has a has many relationship to another domain class CoachProperty.
Hibernate/Grails is creating a third joined table in the database.
In the example below I am trying to fetch the coaches which both have foo AND bar for their text value. I have tried different solutions with 'or' and 'and' in Grails which either returns an empty list or a list with BOTH foo and bar.
Coach:
class Coach {
static hasMany = [ coachProperties : CoachProperty ]
CoachProperty:
class CoachProperty {
String text
boolean active = true
static constraints = {
text(unique: true, nullable: false, blank: false)
}
}
Joined table which is being auto-created and I populated with some data, in this example I am trying to fetch coach 372 since that coach has both 1 and 2 i.e foo and bar:
+---------------------------+-------------------+
| coach_coach_properties_id | coach_property_id |
+---------------------------+-------------------+
| 150 | 2 |
| 372 | 1 |
| 372 | 2 |
| 40 | 3 |
+---------------------------+-------------------+
Inside Coach.createCriteria().list() among with other filters. This should return coach 372 but return empty:
def tempList = ["foo", "bar"]
coachProperties{
for(String temp: tempList){
and {
log.info "temp = " + temp
ilike("text",temp)
}
}
}
I seem to remember this error. Was something about not being able to use both nullable & blank at the same time.Try with just 'nullable:true'
I had to create a workaround with executeQuery where ids is the list containing the id's of the coachproperties i was trying to fetch.
def coaches = Coach.executeQuery '''
select coach from Coach as coach
join coach.coachProperties as props
where props.id in :ids
group by coach
having count(coach) = :count''', [ids: ids.collect { it.toLong()
}, count: ids.size().toLong()]
or{
coaches.each{
eq("id", it.id)
}
}

Fetch Parent even when child is absent

After many attempts I'm here to seek a help from any of you. A solution is very much needed.
I have a parent (Patient) entity and its child (Address) entity.
Where I want to fetch all the registered patients and address details, where address details may be null. Meaning, Patient may not have address details..
I have written logic by using CriteriaBuilder like this
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Object[]> query = builder.createQuery(Object.class);
Root<TaskImpl> patientRoot = query.from(Patient.class);
Root<ContentImpl> addressRoot = query.from(Address.class);
query.multiselect(patientRoot.get("patinetId"),patientRoot .get("patinetName"),
addressRoot.get("city));
Predicate patAddressJoinPred = builder.equal(
patientRoot.get("patientId"),
addressRoot.get("patient").get("patientId"));
query.where(builder.and(patAddressJoinPred));
please find Patient and Address entities for your ref,
Patient.java-----------------
#Entity
public class Patinet{
#Id
private Long patentId;
private String patientName;
}
------------------------------
Address.java------------------
#Entity
public class Address{
#Id
private Long addressId;
private String city;
#OneToOne(FetchType.Lazy)
#JoinColumn("patinet_id")
private Patient patient;
}
-------------------------------
But after the criteria builder, I have applied cross join on Address entity which will be a performance problem and I cannot get details of patients which don't have address details..
for simplicity, the sample data and my required output is given bellow.
Patient table
-------------
id | name
-------------
1 | Sameul
2 | Jhon
3 | khan
4 | Lee
-------------
Address table
-----------------------
id | city | patient_id
-----------------------
1 | Blz | 1
2 | Stn | 3
required out put
-------------------------
id | patientName | city
-------------------------
1 | Sameul | blz
2 | Jhon |
3 | khan | stn
4 | Lee |
But getting like this
-------------------------
id | patientName | city
-------------------------
1 | Sameul | blz
3 | khan | stn
-------------------------
Will be waiting for your valuable solution
Thank you..
It looks you are using inner join, you should use outer joins
Something like,
final Root<Patient> patient = criteriaQuery.from(Patient.class);
Join<Patient, Address> join1 = patient .join("joinColumnName", JoinType.LEFT);
Predicate predicate = criteriaBuilder.equal(Address.<String> get("patient_id"), patient;
criteria.add(predicate);
criteriaQuery.where(predicate);
criteriaQuery.distinct(true);
Hope it resolves your query. I have not tested it but should work fine.

mySQL - Get last added ID [duplicate]

This question already has answers here:
How to get the insert ID in JDBC?
(14 answers)
Closed 4 years ago.
Im a new programmer (just started on my programmer education).
Im trying to set my "tempVenueId" attribute with the last ID in my SQL DB: (in this case idvenue = 12)
Table name: venue
idvenue | name | address
-------------------------------
1 | Khar | 5
2 | SantaCruz | 3
3 | Sion | 2
4 | VT | 1
5 | newFort | 3
6 | Bandra | 2
7 | Worli | 1
8 | Sanpada | 3
9 | Joe | 2
10 | Sally | 1
11 | Elphiston | 2
12 | Currey Road | 1
My code:
Private int tempVenueId;
SqlRowSet rs1 = jdbc.queryForRowSet("SELECT MAX(idvenue) FROM venue;");
while(rs1.next())tempVenueId = rs1.getInt(0);
/*I have also tried with "while(rs1.next())tempVenueId = rs1.getInt(1);"
*/
Still it does not work I get this when I run debug mode in intelliJ:
tempVenueId = 0
The index is from 1 rather than 0,so two ways to solve it:
while(rs1.next()){
tempVenueId = rs1.getInt(1);
break;
};
or
SqlRowSet rs1 = Jdbc.queryForRowSet("SELECT MAX(idvenue) as value FROM venue;");
while(rs1.next()){
tempVenueId = rs1.getInt("value");
break;
}
Solved - This solution was alot easier.... (I think).
String sql = "SELECT LAST_INSERT_ID();";
SqlRowSet rs = Jdbc.queryForRowSet(sql);
rs.next();
Venue VN = new Venue(rs.getInt(1));
tempVenueId = VN;
Now i get the last AUTO INCREMENTET ID from DB

Read array of Json-Ld Objects into a Model In Apache Jena.How to retrieve individual objects from Model?

This is Json Array of Object(Student Data) . I am loaded that Json-Ld Data in Jena Model
[
{
"#context" : {
"myvocab" : "http://mywebsite.com/vocab/",
"name" : "myvocab:name",
"firstname" : "myvocab:firstname",
"lastname" : "myvocab:lastname",
"rollNumber" : "myvocab:rollNumber"
},
"name" : {
"firstname" : "Dhannan",
"lastname" : "Chaudhary"
},
"rollNumber" : "26"
},
{
"#context" : {
"myvocab" : "http://mywebsite.com/vocab/",
"name" : "myvocab:name",
"firstname" : "myvocab:firstname",
"lastname" : "myvocab:lastname",
"rollNumber" : "myvocab:rollNumber"
},
"name" : {
"firstname" : "Maakin",
"lastname" : "Dhayaal"
},
"rollNumber" : "69"
}
]
This is my model output for above example ( by using SPARQL )
-------------------------------------------------------------------
| Subject | Predicate | Object |
===================================================================
| _:b0 | <http://mywebsite.com/vocab/lastname> | "Chaudhary" |
| _:b0 | <http://mywebsite.com/vocab/firstname> | "Dhannan" |
| _:b1 | <http://mywebsite.com/vocab/lastname> | "Dhayaal" |
| _:b1 | <http://mywebsite.com/vocab/firstname> | "Maakin" |
| _:b2 | <http://mywebsite.com/vocab/rollNumber> | "62" |
| _:b2 | <http://mywebsite.com/vocab/name> | _:b1 |
| _:b3 | <http://mywebsite.com/vocab/rollNumber> | "61" |
| _:b3 | <http://mywebsite.com/vocab/name> | _:b0 |
-------------------------------------------------------------------
From this model I want only Subjects(Resources in term of Jena) of every Student for my case it should ( _:b2 , _:b3) .
But by using model.listSubjects() it gives iterator to all subjects ( _:b0 , _:b1 , _:b2 , _:b3)
My main goal is to be able to get individual models for student 1 and student 2.
How can I achieve this?
Every Suggestions are welcome.
First, you can use RDF Type literal to define Student class as well as the StudentName class (not sure why you'd need to break them up).
You can then check if the subject has the property that you are looking for. You can see how we do this in Eclipse Lyo Jena provider.
Finally, you can model your domain with Lyo modelling tools and generate the POJOs for your domain that can be converted from/to Jena models in a single method call.

How to Create A HashMap using Key Values from a text file?

I have to create a hashmap with the names I have in this text file
relationships text file:
Susan Sarandon | Tom Hanks : Cloud Atlas
Tom Hanks | Kevin Bacon : Apollo 13
Leonardo Dicaprio | Kevin Bacon : This Boy's Life
Robert De Niro | Kevin Bacon : This Boy's Life
Barack Obama | Tom Hanks : the Road We've Traveled
Helen Keller | Katharine Cornell : Helen Keller in Her Story
Katharine Cornell | Helen Hayes : Stage Door Canteen
Helen Hayes | John Laughlin : Murder with Mirrors
John Laughlin | Kevin Bacon : Footloose
Mark Zuckerberg | Joe Lipari : Terms and Conditions May Apply
Joe Lipari | Welker White : Eat Pray Love
Welker White | Kevin Bacon : Lemon Sky
This is the program I have now:
public static void main(String[] args)
throws FileNotFoundException
{
Scanner input = new Scanner(new File("relationships"));
HashMap<String, String> relationships = new HashMap<String, String>();
while (input.hasNextLine()) {
String[] columns = input.nextLine().split(" ");
relationships.put(columns[0], columns[1]);
}
System.out.println(relationships);
}
This is the output:
{Leonardo=Dicaprio, Katharine=Cornell, Joe=Lipari, Tom=Hanks, Robert=De, Susan=Sarandon, John=Laughlin, Mark=Zuckerberg, Barack=Obama, Welker=White, Helen=Hayes}
Does anyone know how to fix this please? Also how to seperate them so it actually looks like a list?
EDIT
I think you would just change your line:
String[] columns = input.nextLine().split(" ");
to:
String[] columns = input.nextLine().split(Pattern.quote(" | "));
Then column[0] would be the name on the left, and column[1] would be the name and movie title on the right.
Note that you'll need to import java.util.regex.Pattern; to do this

Categories