All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deepsense.neptune.clientlibrary.models.impl.tags.OnlineTags Maven / Gradle / Ivy

/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.models.impl.tags;

import com.google.common.collect.ImmutableSet;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.clientlibrary.models.Tags;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.utils.exceptions.ApiExceptionWrapper;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.UUID;

public class OnlineTags extends AbstractSet implements Tags {

    private final ApiService apiService;

    private final UUID jobId;

    public OnlineTags(ApiService apiService, UUID jobId) {
        this.apiService = apiService;
        this.jobId = jobId;
    }

    @Override
    public Iterator iterator() {
        return ImmutableSet.copyOf(getAllTags()).iterator();
    }

    @Override
    public int size() {
        return getAllTags().size();
    }

    @Override
    public boolean add(String newTag) {
        Set tags = getAllTags();
        boolean tagsChanged = tags.add(newTag);

        if (tagsChanged) {
            updateTags(tags);
        }
        return tagsChanged;
    }

    @Override
    public boolean remove(Object o) {
        Set tags = getAllTags();
        boolean tagsChanged = tags.remove(o);

        if (tagsChanged) {
            updateTags(tags);
        }
        return tagsChanged;
    }

    @Override
    public boolean addAll(Collection newTags) {
        Set tags = getAllTags();
        boolean tagsChanged = tags.addAll(newTags);

        if (tagsChanged) {
            updateTags(tags);
        }
        return tagsChanged;
    }

    @Override
    public boolean removeAll(Collection c) {
        Set tags = getAllTags();
        boolean tagsChanged = tags.removeAll(c);

        if (tagsChanged) {
            updateTags(tags);
        }
        return tagsChanged;
    }

    @Override
    public boolean retainAll(Collection c) {
        Set tags = getAllTags();
        boolean tagsChanged = tags.retainAll(c);

        if (tagsChanged) {
            updateTags(tags);
        }
        return tagsChanged;
    }

    @Override
    public void clear() {
        updateTags(Collections.emptySet());
    }

    private Set getAllTags() {
        try {
            return new HashSet<>(apiService.getJob(jobId).getTags());
        } catch (ApiException exc) {
            throw ApiExceptionWrapper.wrappedApiException(exc);
        }
    }

    private void updateTags(Set newTags) {
        try {
            apiService.updateTags(jobId, new LinkedList<>(newTags));
        } catch (ApiException exc) {
            throw ApiExceptionWrapper.wrappedApiException(exc);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy