<commit_before>/*
 * Copyright (C) 2015 <NAME>
 *
 * 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 com.elopteryx.paint.upload.internal;

import com.elopteryx.paint.upload.PartOutput;
import com.elopteryx.paint.upload.PartStream;
import com.elopteryx.paint.upload.UploadContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

/**
 * Meta data holder of an upload operation. Its fields are
 * managed by the handler objects.
 */
public class UploadContextImpl implements UploadContext {

    /**
     * The request containing the bytes of the file. It's always a multipart, POST request.
     */
    private final HttpServletRequest request;
    /**
     * The user object. Only used by the callers, not necessary for these classes.
     */
    private final Object userObject;
    /**
     * The currently processed item. 
     */
    private PartStreamImpl currentPart;
    /**
     * The active output.
     */
    private PartOutput output;
    /**
     * The list of the already processed items.
     */
    private final List<PartStream> partStreams = new ArrayList<>();
    /**
     * Determines whether the current item is buffering, that is, should new bytes be
     * stored in memory or written out the channel. It is set to false after the
     * part begin function is called.
     */
    private boolean buffering = true;
    /**
     * The total number for the bytes read for the current part.
     */
    private int partBytesRead;

    public UploadContextImpl(HttpServletRequest request, Object userObject) {
        this.request = request;
        this.userObject = userObject;
    }

    @Override
    @Nonnull
    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public <T> T getUserObject(Class<T> clazz) {
        return userObject != null ? clazz.cast(userObject) : null;
    }

    @Override
    @Nonnull
    public PartStream getCurrentPart() {
        return currentPart;
    }

    @Override
    @Nullable
    public PartOutput getCurrentOutput() {
        return output;
    }

    @Nonnull
    @Override
    public List<PartStream> getPartStreams() {
        return Collections.unmodifiableList(partStreams);
    }

    void reset(PartStreamImpl newPart) {
        buffering = true;
        partBytesRead = 0;
        currentPart = newPart;
        partStreams.add(newPart);
        output = null;
    }

    void setOutput(PartOutput output) {
        this.output = output;
        this.currentPart.setOutput(output);
    }

    boolean isBuffering() {
        return buffering;
    }

    void finishBuffering() {
        buffering = false;
        currentPart.markAsFinished();
    }

    void updatePartBytesRead() {
        currentPart.setSize(partBytesRead);
    }

    int getPartBytesRead() {
        return partBytesRead;
    }

    int incrementAndGetPartBytesRead(int additional) {
        partBytesRead += additional;
        return partBytesRead;
    }
}
<commit_msg>Javadoc fix.<commit_after>/*
 * Copyright (C) 2015 <NAME>
 *
 * 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 com.elopteryx.paint.upload.internal;

import com.elopteryx.paint.upload.PartOutput;
import com.elopteryx.paint.upload.PartStream;
import com.elopteryx.paint.upload.UploadContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

/**
 * Default implementation of {@link com.elopteryx.paint.upload.UploadContext}.
 */
public class UploadContextImpl implements UploadContext {

    /**
     * The request containing the bytes of the file. It's always a multipart, POST request.
     */
    private final HttpServletRequest request;
    /**
     * The user object. Only used by the callers, not necessary for these classes.
     */
    private final Object userObject;
    /**
     * The currently processed item. 
     */
    private PartStreamImpl currentPart;
    /**
     * The active output.
     */
    private PartOutput output;
    /**
     * The list of the already processed items.
     */
    private final List<PartStream> partStreams = new ArrayList<>();
    /**
     * Determines whether the current item is buffering, that is, should new bytes be
     * stored in memory or written out the channel. It is set to false after the
     * part begin function is called.
     */
    private boolean buffering = true;
    /**
     * The total number for the bytes read for the current part.
     */
    private int partBytesRead;

    public UploadContextImpl(HttpServletRequest request, Object userObject) {
        this.request = request;
        this.userObject = userObject;
    }

    @Override
    @Nonnull
    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public <T> T getUserObject(Class<T> clazz) {
        return userObject != null ? clazz.cast(userObject) : null;
    }

    @Override
    @Nonnull
    public PartStream getCurrentPart() {
        return currentPart;
    }

    @Override
    @Nullable
    public PartOutput getCurrentOutput() {
        return output;
    }

    @Nonnull
    @Override
    public List<PartStream> getPartStreams() {
        return Collections.unmodifiableList(partStreams);
    }

    void reset(PartStreamImpl newPart) {
        buffering = true;
        partBytesRead = 0;
        currentPart = newPart;
        partStreams.add(newPart);
        output = null;
    }

    void setOutput(PartOutput output) {
        this.output = output;
        this.currentPart.setOutput(output);
    }

    boolean isBuffering() {
        return buffering;
    }

    void finishBuffering() {
        buffering = false;
        currentPart.markAsFinished();
    }

    void updatePartBytesRead() {
        currentPart.setSize(partBytesRead);
    }

    int getPartBytesRead() {
        return partBytesRead;
    }

    int incrementAndGetPartBytesRead(int additional) {
        partBytesRead += additional;
        return partBytesRead;
    }
}

{'fraction_non_alphanumeric': 0.08321775312066575, 'fraction_numerical': 0.0024965325936199723, 'mean_word_length': 2.7362694300518133, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '536209', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1931, 'n_words': 639}