f81e8f664388589ea2a2b576af1a307bc6014ffd
[JavaForFun] /
1 package de.spring.webservices.rest.business.service.impl;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.concurrent.atomic.AtomicLong;
7
8 import org.springframework.data.domain.Page;
9 import org.springframework.data.domain.PageImpl;
10 import org.springframework.data.domain.Pageable;
11 import org.springframework.stereotype.Service;
12
13 import de.spring.webservices.domain.Car;
14 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
15 import rx.exceptions.Exceptions;
16
17 @Service("awesomeBusinessLogic")
18 public class AwesomeBusinessLogicImpl implements AwesomeBusinessLogic {
19         private static final String TEMPLATE = "Car: %s";
20         
21     private final AtomicLong counter = new AtomicLong();
22         
23         @Override
24         public Page<Car> findAll(Pageable pageRequest) {
25         final List<Car> cars = new ArrayList<>();
26         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
27         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
28         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
29         
30         try {
31             Thread.sleep(10000);
32         } catch(InterruptedException ex) {
33             Thread.currentThread().interrupt();
34         }
35                 
36                 return new PageImpl<>(cars);    
37         }
38
39         @Override
40         public Car findById(long id) {
41                 
42         try {
43             Thread.sleep(10000);
44         } catch(InterruptedException ex) {
45             Thread.currentThread().interrupt();
46         }
47                 
48                 
49                 return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
50         }
51
52         @Override
53         public Car create(Car car) {
54                 long count = counter.incrementAndGet();
55                 
56         try {
57             Thread.sleep(10000);
58         } catch(InterruptedException ex) {
59             Thread.currentThread().interrupt();
60         }
61         
62                 return new Car(count, String.format(TEMPLATE, count));
63         }
64         
65         @Override
66         public Car createThrowable(Car car) throws IOException {
67                 
68                 throw new IOException("createThrowable FATAL ERROR");
69                 // Spring sees both exceptions.
70                 // It seems like calling Exceptions.propagate(ex) in RxJava code is only required
71                 // for no RuntimeExceptions :/
72                 // throw new RuntimeException("createThrowable FATAL ERROR");
73         }
74 }