<commit_before>/*
 * Copyright (C) 2011 <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 org.lugatgt.zoogie.present;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;


/**
 * Base class for presentations.
 * @author <NAME>
 */
public abstract class Presentation {
    
    private static final String PROP_PFX = "presentation.";
    private static final String PROP_INDEX = PROP_PFX + "index";
    
    private List<Slide> slides;
    private Map<String, Slide> nameToSlide;
    
    private OnIndexChangedListener indexChangedListener;
    
    private int idx;
    
    // CONSTRUCTORS ////////////////////////////////////////////////////////////
    
    /**
     * Constructor.
     * @param initSlides The initial set of slides (may not be null, may not be empty).
     */
    public Presentation(Slide[] initSlides) {
        if (initSlides == null || initSlides.length == 0) {
            throw new IllegalArgumentException("Initial slides must not be empty.");
        }
        
        slides = Arrays.asList(initSlides);
        
        nameToSlide = new HashMap<String, Slide>();
        for (Slide initSlide : initSlides) {
            nameToSlide.put(initSlide.getName(), initSlide);
        }
        
        idx = 0;
    }
    
    // FIELD ACCESS ////////////////////////////////////////////////////////////
    
    /**
     * Set the callback for when the current slide index changes.
     * @param listener The listener (may be null).
     */
    public void setOnIndexChangedListener(OnIndexChangedListener listener) {
        indexChangedListener = listener;
    }
    
    // SLIDES //////////////////////////////////////////////////////////////////
    
    public CharSequence[] getSlideTitles(Context ctx) {
        CharSequence[] titles = new CharSequence[slides.size()];
        for (int i = 0; i < slides.size(); i++) {
            titles[i] = slides.get(i).getTitle(ctx);
        }
        return titles;
    }
    
    public Slide getCurrentSlide() {
        return slides.get(idx);
    }
    
    public int getCurrentSlideIndex() {
        return idx;
    }
    
    public boolean isAtBeginning() {
        return idx == 0;
    }
    
    public boolean isAtEnd() {
        return idx == slides.size() - 1;
    }
    
    public int getSlideCount() {
        return slides.size();
    }
    
    public Slide jumpTo(int i) {
        if (i < 0 || i >= slides.size()) {
            throw new ArrayIndexOutOfBoundsException(
                "Slide index must be 0 <= idx < " + slides.size());
        }
        Slide oldSlide = getCurrentSlide();
        int oldIdx = idx;
        idx = i;
        fireOnIndexChanged(oldSlide, oldIdx, true);
        return getCurrentSlide();
    }
    
    public Slide prev() {
        if (idx == 0) {
            return null;
        } else {
            Slide oldSlide = getCurrentSlide();
            idx--;<commit_msg>Add ability to jump to a slide by name.<commit_after>/*
 * Copyright (C) 2011 <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 org.lugatgt.zoogie.present;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;


/**
 * Base class for presentations.
 * @author <NAME>
 */
public abstract class Presentation {
    
    private static final String PROP_PFX = "presentation.";
    private static final String PROP_INDEX = PROP_PFX + "index";
    
    private List<Slide> slides;
    private Map<String, Integer> nameToIndex;
    
    private OnIndexChangedListener indexChangedListener;
    
    private int idx;
    
    // CONSTRUCTORS ////////////////////////////////////////////////////////////
    
    /**
     * Constructor.
     * @param initSlides The initial set of slides (may not be null, may not be empty).
     */
    public Presentation(Slide[] initSlides) {
        if (initSlides == null || initSlides.length == 0) {
            throw new IllegalArgumentException("Initial slides must not be empty.");
        }
        
        slides = Arrays.asList(initSlides);
        
        nameToIndex = new HashMap<String, Integer>();
        for (int i = 0; i < initSlides.length; i++) {
            Slide initSlide = initSlides[i];
            nameToIndex.put(initSlide.getName(), i);
        }
        
        idx = 0;
    }
    
    // FIELD ACCESS ////////////////////////////////////////////////////////////
    
    /**
     * Set the callback for when the current slide index changes.
     * @param listener The listener (may be null).
     */
    public void setOnIndexChangedListener(OnIndexChangedListener listener) {
        indexChangedListener = listener;
    }
    
    // SLIDES //////////////////////////////////////////////////////////////////
    
    public CharSequence[] getSlideTitles(Context ctx) {
        CharSequence[] titles = new CharSequence[slides.size()];
        for (int i = 0; i < slides.size(); i++) {
            titles[i] = slides.get(i).getTitle(ctx);
        }
        return titles;
    }
    
    public Slide getCurrentSlide() {
        return slides.get(idx);
    }
    
    public int getCurrentSlideIndex() {
        return idx;
    }
    
    public boolean isAtBeginning() {
        return idx == 0;
    }
    
    public boolean isAtEnd() {
        return idx == slides.size() - 1;
    }
    
    public int getSlideCount() {
        return slides.size();
    }
    
    public Slide jumpTo(int i) {
        if (i < 0 || i >= slides.size()) {
            throw new ArrayIndexOutOfBoundsException(
                "Slide index must be 0 <= idx < " + slides.size());
        }
        Slide oldSlide = getCurrentSlide();
        int oldIdx = idx;
        idx = i;
        fireOnIndexChanged(oldSlide, oldIdx, true);
        return getCurrentSlide();
    }
    
    public Slide jumpTo(String name) {
        Integer newIdx = nameToIndex.get(name);
        if (newIdx == null) {
            throw new IllegalArgumentException("Unable to find a slide named \"" + name + '"');
        } else {
            return jumpTo(newIdx);
        }
    }
    
    public Slide prev() {
        if (idx == 0) {
            return null;
        } else {
            Slide oldSlide = getCurrentSlide();
            idx--;
{'fraction_non_alphanumeric': 0.14445643377394118, 'fraction_numerical': 0.004451038575667656, 'mean_word_length': 1.84535686876439, 'pattern_counts': {'":': 0, '<': 22, '<?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': '7657575', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1998, 'n_words': 631}