io.pivotal.strepsirrhini.chaoslemur.task.TaskManager.java Source code

Java tutorial

Introduction

Here is the source code for io.pivotal.strepsirrhini.chaoslemur.task.TaskManager.java

Source

/*
 * Copyright 2014-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.pivotal.strepsirrhini.chaoslemur.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/task")
final class TaskManager implements TaskRepository {

    private final AtomicLong counter = new AtomicLong();

    private final TaskResourceAssembler resourceAssembler;

    private final Map<Long, Task> tasks = new ConcurrentHashMap<>();

    @Autowired
    TaskManager(TaskResourceAssembler resourceAssembler) {
        this.resourceAssembler = resourceAssembler;
    }

    @Override
    public Task create(Trigger trigger) {
        Task task = new Task(this.counter.getAndIncrement(), trigger);
        this.tasks.put(task.getId(), task);
        return task;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<?> read(@PathVariable Long id) {
        Task task = this.tasks.get(id);
        if (task == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<>(this.resourceAssembler.toResource(task), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.GET, value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    Set<Resource<Task>> readAll() {
        return this.tasks.values().stream().map(this.resourceAssembler::toResource).collect(Collectors.toSet());
    }

}