<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.chain.web.servlet;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.chain.web.MapEntry;


/**
 * <p>Private implementation of <code>Map</code> for HTTP session
 * attributes.</p>
 *
 * @author <NAME>
 * @version $Revision$ $Date$
 */

final class ServletSessionScopeMap implements Map<String, Object> {


    public ServletSessionScopeMap(HttpServletRequest request) {
        this.request = request;
        sessionExists();
    }


    private HttpSession session = null;
    private HttpServletRequest request = null;


    public void clear() {
        if (sessionExists()) {
            for (String key : keySet()) {
                session.removeAttribute(key);
            }
        }
    }


    public boolean containsKey(Object key) {
        if (sessionExists()) {
            return (session.getAttribute(key(key)) != null);
        } else {
            return false;
        }
    }


    public boolean containsValue(Object value) {
        if (value == null || !sessionExists()) {
            return (false);
        }
        Enumeration<String> keys = session.getAttributeNames();
        while (keys.hasMoreElements()) {
            Object next = session.getAttribute(keys.nextElement());
            if (value.equals(next)) {
                return (true);
            }
        }
        return (false);
    }


    public Set<Entry<String, Object>> entrySet() {
        Set<Entry<String, Object>> set = new HashSet<Entry<String, Object>>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            String key;
            while (keys.hasMoreElements()) {
                key = keys.nextElement();
                set.add(new MapEntry(key, session.getAttribute(key), true));
            }
        }
        return (set);
    }


    public boolean equals(Object o) {
        if (sessionExists()) {
            return (session.equals(o));
        } else {
            return false;
        }
    }


    public Object get(Object key) {
        if (sessionExists()) {
            return (session.getAttribute(key(key)));
        } else {
            return null;
        }
    }


    public int hashCode() {
        if (sessionExists()) {
            return (session.hashCode());
        } else {
            return 0;
        }
    }


    public boolean isEmpty() {
        if (sessionExists() &&
            session.getAttributeNames().hasMoreElements()) {
            return false;
        } else {
            return true;
        }
    }


    public Set<String> keySet() {
        Set<String> set = new HashSet<String>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                set.add(keys.nextElement());
            }
        }
        return (set);
    }


    public Object put(String key, Object value) {
        if (value == null) {
            return (remove(key));
        }

        // Ensure the Session is created, if it
        // doesn't exist
        if (session == null) {
            session = request.getSession();
            request = null;
        }

        Object previous = session.getAttribute(key);
        session.setAttribute(key, value);
        return (previous);
    }


    public void putAll(Map<? extends String, ? extends Object> map) {
        for (Entry<? extends String, ? extends Object> entry : map.entrySet()) {
            put(key(entry.getKey()), entry.getValue());
        }
    }


    public Object remove(Object key) {
        if (sessionExists()) {
            String skey = key(key);
            Object previous = session.getAttribute(skey);
            session.removeAttribute(skey);
            return (previous);
        } else {
            return (null);
        }
    }


    public int size() {
        int n = 0;
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                keys.nextElement();
                n++;
            }
        }
        return (n);
    }


    public Collection<Object> values() {
        List<Object> list = new ArrayList<Object>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                list.add(session.getAttribute(keys.nextElement()));
            }
        }
        return (list);
    }


    private String key(Object key) {
        if (key == null) {
            throw new IllegalArgumentException();
        } else if (key instanceof String) {
            return ((String) key);
        } else {
            return (key.toString());
        }
    }

    private boolean sessionExists() {
        if (session == null) {
            session = request.getSession(false);
            if (session != null) {
                request = null;
            }
        }
        if (session != null) {
            return true;
        } else {
            return false;
        }
    }

}<commit_msg>fixed checkstyle violations: Conditional logic can be removed.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.chain.web.servlet;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.chain.web.MapEntry;


/**
 * <p>Private implementation of <code>Map</code> for HTTP session
 * attributes.</p>
 *
 * @author <NAME>
 * @version $Revision$ $Date$
 */

final class ServletSessionScopeMap implements Map<String, Object> {


    public ServletSessionScopeMap(HttpServletRequest request) {
        this.request = request;
        sessionExists();
    }


    private HttpSession session = null;
    private HttpServletRequest request = null;


    public void clear() {
        if (sessionExists()) {
            for (String key : keySet()) {
                session.removeAttribute(key);
            }
        }
    }


    public boolean containsKey(Object key) {
        if (sessionExists()) {
            return (session.getAttribute(key(key)) != null);
        } else {
            return false;
        }
    }


    public boolean containsValue(Object value) {
        if (value == null || !sessionExists()) {
            return (false);
        }
        Enumeration<String> keys = session.getAttributeNames();
        while (keys.hasMoreElements()) {
            Object next = session.getAttribute(keys.nextElement());
            if (value.equals(next)) {
                return (true);
            }
        }
        return (false);
    }


    public Set<Entry<String, Object>> entrySet() {
        Set<Entry<String, Object>> set = new HashSet<Entry<String, Object>>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            String key;
            while (keys.hasMoreElements()) {
                key = keys.nextElement();
                set.add(new MapEntry(key, session.getAttribute(key), true));
            }
        }
        return (set);
    }


    public boolean equals(Object o) {
        if (sessionExists()) {
            return (session.equals(o));
        } else {
            return false;
        }
    }


    public Object get(Object key) {
        if (sessionExists()) {
            return (session.getAttribute(key(key)));
        } else {
            return null;
        }
    }


    public int hashCode() {
        if (sessionExists()) {
            return (session.hashCode());
        } else {
            return 0;
        }
    }


    public boolean isEmpty() {
        if (sessionExists() &&
            session.getAttributeNames().hasMoreElements()) {
            return false;
        }
        return true;
    }


    public Set<String> keySet() {
        Set<String> set = new HashSet<String>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                set.add(keys.nextElement());
            }
        }
        return (set);
    }


    public Object put(String key, Object value) {
        if (value == null) {
            return (remove(key));
        }

        // Ensure the Session is created, if it
        // doesn't exist
        if (session == null) {
            session = request.getSession();
            request = null;
        }

        Object previous = session.getAttribute(key);
        session.setAttribute(key, value);
        return (previous);
    }


    public void putAll(Map<? extends String, ? extends Object> map) {
        for (Entry<? extends String, ? extends Object> entry : map.entrySet()) {
            put(key(entry.getKey()), entry.getValue());
        }
    }


    public Object remove(Object key) {
        if (sessionExists()) {
            String skey = key(key);
            Object previous = session.getAttribute(skey);
            session.removeAttribute(skey);
            return (previous);
        } else {
            return (null);
        }
    }


    public int size() {
        int n = 0;
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                keys.nextElement();
                n++;
            }
        }
        return (n);
    }


    public Collection<Object> values() {
        List<Object> list = new ArrayList<Object>();
        if (sessionExists()) {
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                list.add(session.getAttribute(keys.nextElement()));
            }
        }
        return (list);
    }


    private String key(Object key) {
        if (key == null) {
            throw new IllegalArgumentException();
        } else if (key instanceof String) {
            return ((String) key);
        } else {
            return (key.toString());
        }
    }

    private boolean sessionExists() {
        if (session == null) {
            session = request.getSession(false);
            if (session != null) {
                request = null;
            }
        }
        if (session != null) {
            return true;
        }
        return false;
    }

}
{'fraction_non_alphanumeric': 0.11052503448835511, 'fraction_numerical': 0.0009737888501176661, 'mean_word_length': 1.8936370039915473, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '6081047', 'n_tokens_mistral': 3196, 'n_tokens_neox': 3113, 'n_words': 899}